如何检查字符串是否以Java中的特定子字符串开头?
一个字符串 类可以用来表示字符串,在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!
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短