Parameters of Java String startsWith()
Prefix: Prefix is the sequence of characters that is to be matched with the given string to check whether it starts from it.
Offset: It gives us the starting position from where the matching of the string prefix start. It is optional and if not given, the matching will start from the first character.
Syntax of String startsWith() in Java
The syntax of String startsWith is:
If offset is not given
Public boolean startsWith(string prefix)
If offset is given
Public boolean startsWith(string prefix, int offset)
Return value of String startsWith() in Java
A boolean value that returns
true: if the string begins with the given prefix.
false: if the string does not begin with the given prefix.
Implementation of String startswith() without offset
import java.util.*;
class codingninjas {
public static void main(String[] args)
{
//initialising the string
String str = new String(
"welcome to coding ninjas");
//giving the prefix
boolean ans1 = str.startsWith("welcome");
boolean ans2 = str.startsWith("coding");
boolean ans3 = str.startsWith("we");
boolean ans4 = str.startsWith("welcome to coding ninjas");
// Printing the boolean value
System.out.println(ans1);
System.out.println(ans2);
System.out.println(ans3);
System.out.println(ans4);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
true
false
true
true
Implementation of String startswith() with offset
import java.util.*;
class codingninjas {
public static void main(String[] args)
{
//initialising the string
String str = new String(
"welcome to coding ninjas");
//giving the prefix
boolean ans1 = str.startsWith("ninjas",18);
boolean ans2 = str.startsWith("ninjas",10);
boolean ans3 = str.startsWith("coding ninjas", 11);
boolean ans4 = str.startsWith("we", 1);
boolean ans5 = str.startsWith("welcome to coding ninjas", 0);
// Printing the boolean value
System.out.println(ans1);
System.out.println(ans2);
System.out.println(ans3);
System.out.println(ans4);
System.out.println(ans5);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
true
false
true
false
true
Frequently Asked Questions
Where can we use string startsWith()?
String startsWith() can be used to filter prefixes. For example, we can use String startsWith() to filter names according to a particular letter.
What does startsWith () do?
The startsWith() method in Java checks if a string begins with a specified prefix and returns true if it does, otherwise false.
Is startsWith case sensitive in Java?
Yes, startsWith() is case-sensitive in Java, meaning it distinguishes between uppercase and lowercase letters when matching the prefix.
What is the difference between charAt and startsWith?
charAt() retrieves a specific character at a given index in a string, while startsWith() checks if a string starts with a particular prefix. charAt() returns a character, and startsWith() returns a boolean.
Conclusion
The String startsWith() in Java is a simple and effective way to check if a string begins with a specified prefix. It returns a boolean, offering quick validation and pattern matching. Its flexibility with optional position arguments and ease of use make it a valuable tool for string manipulation, improving code readability and efficiency.
Refer to our Guided Path on Code360 to upskill yourself! If you want to test your competency in coding, you may check out the mock test series and participate in the contests.