String class can be used to represent strings. All string literals in Java programs are implemented as an instance A string class. Strings are constants, and their values cannot be changed once created (immutable).
We can use the startsWith(String class) method to check if a string starts with a specific string, it returns a boolean value, true or false .
Syntaxpublic boolean startsWith(String prefix)
public class StringStartsWithSubStringTest { public static void main(String[] args) { String str = "WelcomeToTutorialsPoint"; if(str.startsWith("<strong>Welcome</strong>")) { System.out.println("Begins with the specified word!"); } else { System.out.println("Does not begin with the specified word!"); } } }
Begins with the specified word!
The above is the detailed content of How to check if a string starts with a specific substring in Java?. For more information, please follow other related articles on the PHP Chinese website!