⬅ Previous Topic
Java Stringsplit()
methodNext Topic ⮕
Java StringsubSequence()
methodstartsWith()
method⬅ Previous Topic
Java Stringsplit()
methodNext Topic ⮕
Java StringsubSequence()
methodThe startsWith()
method in Java is a handy tool for checking if a string begins with a specified prefix. It's like verifying if a book title starts with a certain word, or confirming that a file name follows a particular pattern.
public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int toffset)
Here's a breakdown of the parameters accepted by startsWith()
:
Parameter | Description |
---|---|
prefix |
The string to check if the original string starts with. |
prefix |
The string to check if the original string starts with. |
toffset |
An offset from which to start checking the prefix. This allows you to skip a certain number of characters at the beginning of the string before comparing. Must be non-negative and less than or equal to the length of this string. |
The startsWith()
method returns a boolean
value:
true
: If the string starts with the specified prefix.false
: Otherwise.This example demonstrates how to use startsWith()
with a simple prefix.
String str = "Hello World";
String prefix = "Hello";
boolean startsWithPrefix = str.startsWith(prefix);
System.out.println(startsWithPrefix); // Output: true
true
In this case, we check if the string str starts with the prefix Hello. Because it does, the method returns true
.
This example highlights that startsWith()
is case-sensitive.
String str = "Hello World";
String prefix = "hello"; // Note the lowercase 'h'
boolean startsWithPrefix = str.startsWith(prefix);
System.out.println(startsWithPrefix);
false
Because the first character of str is 'H' (uppercase), while the prefix starts with 'h' (lowercase), the method returns false
. Case matters!
toffset
ParameterThis example shows how to use the toffset
parameter to ignore a certain number of characters at the beginning of the string.
String str = " Hello World"; // String with leading spaces
String prefix = "Hello";
boolean startsWithPrefix = str.startsWith(prefix, 2); // Check starting from index 2 (skipping the first two characters)
System.out.println(startsWithPrefix);
true
Here, we're checking if the string str starts with Hello, but we're starting our check from index 2. This effectively ignores the two leading spaces. Because the substring starting at index 2 is indeed "Hello", the method returns true
.
This example demonstrates what happens when you use an empty string as a prefix
String str = "Hello World";
String prefix = ""; // An empty String
boolean startsWithPrefix = str.startsWith(prefix);
System.out.println(startsWithPrefix);
true
An empty string is considered a prefix of any string, so the method returns true
.
⬅ Previous Topic
Java Stringsplit()
methodNext Topic ⮕
Java StringsubSequence()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.