EN
Java - string startsWith() method example
0 points
In this article, we would like to show you startsWith()
method example in Java.
Quick solution:
xxxxxxxxxx
1
String string = "Dirask is awesome.";
2
3
System.out.println(string.startsWith("Dirask ")); // true
4
System.out.println(string.startsWith("is")); // false
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. |
In this example, we check if our string
starts with a specified character or string.
xxxxxxxxxx
1
public class Example {
2
public static void main(String[] args) {
3
String string = "Dirask is awesome.";
4
5
System.out.println(string.startsWith("D")); // true
6
System.out.println(string.startsWith("Dirask")); // true
7
8
System.out.println(string.startsWith("X")); // false
9
System.out.println(string.startsWith("is")); // false
10
}
11
}
Output:
xxxxxxxxxx
1
true
2
true
3
false
4
false