The startsWith() method of String class is used for checking prefix of a String. It returns a boolean value true or false based on whether the given string starts with the specified letter or word.
For example:
String str = "Hello"; //This will return true because string str starts with "He" str.startsWith("He");
Java String startsWith() method Examples
There are two variations of starsWith() method.
boolean startsWith(String str)
: It returns true if the String str is a prefix of the String.
boolean startsWith(String str, index fromIndex)
: It returns true if the String begins with str, it starts looking from the specified index “fromIndex”.
For example lets say that the value of the String str is “Hi there” and we are calling starsWith() method like this – str.startsWith(“there”, 3) then this will return true because we have provided the fromIndex value as 3. The method will start looking from index 3 and it will find the string from where it starts searching.
Example 1: Checking prefix without fromIndex
This is a simple example where we have a string s and we are checking wether the string s starts with a particular word using startsWith() method.
public class JavaExample{ public static void main(String args[]){ //given string String s = "This is just a sample string"; //checking whether the given string starts with "This" System.out.println(s.startsWith("This")); //checking whether the given string starts with "Hi" System.out.println(s.startsWith("Hi")); } }
Output:
Example 2: Checking prefix starting from an index
Let’s take an example where we are using both the variations of startsWith() method. As you can see that when we provided the fromIndex, the output changed. This is because it starts looking from the specified fromIndex.
This means:
- If
str.startsWith("brown")
then it is checking the prefix of this string: quick brown fox jumps over the lazy dog - If
str.StartsWith("brown", 6)
then it is checking the prefix of this string: brown fox jumps over the lazy dog
public class StartsWithExample{ public static void main(String args[]) { String str= new String("quick brown fox jumps over the lazy dog"); System.out.println("String str starts with quick: "+str.startsWith("quick")); System.out.println("String str starts with brown: "+str.startsWith("brown")); System.out.println("substring of str(starting from 6th index) has brown prefix: " +str.startsWith("brown", 6)); System.out.println("substring of str(starting from 6th index) has quick prefix: " +str.startsWith("quick", 6)); } }
Output:
String str starts with quick: true String str starts with brown: false substring of str(starting from 6th index) has brown prefix: true substring of str(starting from 6th index) has quick prefix: false
Example 3: Whether String starts with an empty string
An empty string is often referred as “” (no space between double quotes). We are checking whether every string has empty string as a prefix. We can simply check this by calling startsWith() method like this: str.startsWith("")
.
public class JavaExample { public static void main(String args[]) { String str = "Welcome to BeginnersBook"; // "" represents an empty string if(str.startsWith("")){ System.out.println("String has empty string as prefix"); }else{ System.out.println("String doesn't have empty string as prefix"); } } }
Output:
String has empty string as prefix
Srihitha says
will startsWith method return true if the input is:
search string-aab
list-{bcf , nhgggg , aabde}
As per my knowledge it should return true as aabde contains aab.
Am i right?
Prabhu Kiran says
Will it check starting word or starting letter…??
Chaitanya Singh says
It depends on what you are passing in the startsWith() method, for example if you are calling the method like this: str.startsWith(“A”) then it will check whether the string str starts with letter ‘A’ or not.