How do I check a string starts with a specified word?
Category: java.lang, viewed: 4K time(s).
To test if a string has a defined prefix we can use the String.startsWith() method. This method returns a boolean true as the result if the string has the defined prefix.
package org.kodejava.example.lang;
public class StartsWithSample {
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");
}
}
}
More examples on java.lang