Java startsWith: Check String Prefix
In Java, you can use the startsWith() method to check if a string starts with a specified prefix. This method takes a string parameter as the prefix, and returns true if the string starts with the specified prefix, otherwise it returns false. Here is an example code snippet:
String str = "Hello World";
// 判断字符串是否以指定前缀"Hello"开头
if(str.startsWith("Hello")) {
System.out.println("字符串以指定前缀开头");
} else {
System.out.println("字符串不以指定前缀开头");
}
When running the above code, the output will be:
字符串以指定前缀开头