如何检查字符串是否以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!
热门推荐
8 送心的祝福语简短
10 保洁阿姨送来祝福语简短
11 祝福语闺蜜简短单纯
12 简短的爱情生日祝福语
13 朋友儿子点灯祝福语简短
14 祝福语送顾客简短语
15 小学同学简短祝福语
16 给新生的简短祝福语
17 生日奥特曼可爱祝福语简短
18 早上祝福语简短句子