如何检查字符串是否以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!
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短