一个字符串 类可以用来表示字符串,在Java程序中的所有字符串文字被实现为String类的一个实例。字符串是常量,一旦创建,它们的值就不能更改(不可变)。
我们可以使用String类的startsWith()方法检查字符串是否以特定字符串开头,它返回布尔值, true或false。
public boolean startsWith(String prefix)
public class StringStartsWithSubStringTest {
public static void main(String[] args) {
String str = "WelcomeToTutorialsPoint";
if(str.startsWith("Welcome")) {
System.out.println("Begins with the specified word!");
}
else {
System.out.println("Does not begin with the specified word!");
}
}
}输出结果
Begins with the specified word!