如何检查字符串是否以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 红包祝福语简短10字
18 周六早晨祝福语简短