To test if a string starts with a specific word we can use the String.startsWith()
method. This method returns a boolean true
as the result if the string starts with that specific word.
package org.kodejava.example.lang;
public class StringStartsWithExample {
public static void main(String[] args) {
String str = "The quick brown fox jumps over the lazy dog";
// See if the fox is a quick fox.
if (str.startsWith("The quick")) {
System.out.println("Yes, the fox is the quick one");
} else {
System.out.println("The fox is a slow fox");
}
}
}
The code snippet print the following output:
Yes, the fox is the quick one
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020