Java String startsWith() Method
Example
Find out if the string starts with the specified characters:
public class MyClass {
public static void main(String[] args) {
String myStr = "Hello";
System.out.println(myStr.startsWith("Hel")); // true
System.out.println(myStr.startsWith("llo")); // false
System.out.println(myStr.startsWith("o")); // false
}
}
Definition and Usage
The startsWith()
method checks whether a
string starts with the specified character(s).
Tip: Use the endsWith() method to check whether a string ends with the specified character(s).
Syntax
public boolean startsWith(String chars)
Parameter Values
Parameter | Description |
---|---|
chars | A String , representing the character(s) to check for |
Technical Details
Returns: | A boolean value:
|
---|
❮ String Methods