EN
Java - string startsWith() method example
0
points
In this article, we would like to show you startsWith()
method example in Java.
Quick solution:
String string = "Dirask is awesome.";
System.out.println(string.startsWith("Dirask ")); // true
System.out.println(string.startsWith("is")); // false
1. Documentation
Syntax | public boolean startsWith(String prefix) |
Parameters | prefix - the string representation of the character to be checked |
Result | true - if the string starts with the specified character or word, false - if the string doesn't start with the specified character or word. |
Description |
Tests if the string starts with the specified prefix. |
2. Practical example
In this example, we check if our string
starts with a specified character or string.
public class Example {
public static void main(String[] args) {
String string = "Dirask is awesome.";
System.out.println(string.startsWith("D")); // true
System.out.println(string.startsWith("Dirask")); // true
System.out.println(string.startsWith("X")); // false
System.out.println(string.startsWith("is")); // false
}
}
Output:
true
true
false
false