Java String startsWith() Method with Examples
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
The startsWith() method in Java checks if a string starts with a specified prefix or substring. This method validates input strings or filters strings based on a particular starting pattern.
The startsWith() method is defined in the java.lang package, which means it is available to all Java programs by default without needing any additional imports.
The primary purpose of startsWith() is to check if a string begins with a specific sequence of characters. It returns a boolean value – actual if the string starts with the specified prefix, false otherwise.
Variants of Java String startsWith() method
There are two variants of the startsWith() method in Java:
startsWith(String prefix)
This method checks if the string on which it is called starts with the specified prefix.
Syntax:
boolean startsWith(String prefix)
Parameters:
- prefix – The prefix string to check for
Return Type:
- boolean – true if the string starts with a prefix, false otherwise
Example:
public class StartsWithExample {
public static void main(String[] args) {
String str = "Hello World";
boolean starts = str.startsWith("He");
System.out.println("String starts with 'He': " + starts);
}
}Output:
String starts with ‘He’: true
startsWith(String prefix, int strt_pos)
This overloaded version checks if the string starts with the specified prefix from a given starting position in the string.
Syntax:
boolean startsWith(String prefix, int strt_pos)
Parameters:
- prefix – The prefix string
- strt_pos – The starting position in the original string to start checking from
Return Type:
- boolean – true if the substring of the original string starting from strt_pos starts with prefix, false otherwise
Example:
public class StartsWithExample {
public static void main(String[] args) {
String str = "Hello World";
boolean starts = str.startsWith("llo", 2);
System.out.println("String starts with 'llo' at position 2: " + starts);
}
}Output:
String starts with ‘llo’ at position 2: true
Real-Life Example
One real-life use case of startsWith() is to validate user input in an application.
For example, in an app that takes product codes as input, we can check if the input string starts with a valid prefix for product codes before processing it further:
// Product codes start with "PROD" followed by 3 digits
String input = "PROD123";
if(input.startsWith("PROD")) {
// Input is valid - extract product ID
String productId = input.substring(4);
// Query database or perform other logic with productId
} else {
// Invalid input - notify user
}Here, we check if the input string starts with “PROD” to verify it is a valid product code before extracting the product ID from it and performing further actions.
This prevents unwanted errors from invalid input values.
Conclusion
In conclusion, Java’s startsWith() method is a valuable tool for string manipulation and validation. It allows us to efficiently determine whether a string begins with a specified prefix, helping filter and process data effectively. With its two variants, this method offers flexibility in checking for prefixes from different starting positions.
Moreover, in real-life applications, such as user input validation, startsWith() plays a crucial role in enhancing the accuracy and robustness of software by preventing errors caused by improper input.
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

