startswith()
用于检测字符串是否以指定的字符开始
语法:
//prefix:需要指定的字符串 toffset:字符串开始的地方
public boolean startsWith(String prefix, int toffset)
public boolean startsWith(String prefix)
应用场景:带两参数适用于字符串中是否包含某个字符串
带一个参数适用于是否以指定字符串开头
实例:
public class Test01 {
public static void main(String[] args) {
String str = "xiaoLi ";
//ture
System.out.println(str.startsWith("x"));
//true 从第五个字符判断
System.out.println(str.startsWith("L", 4));
}
}
indexOf()
返回值:不包含返回-1
实例:
public class Test02 {
public static void main(String[] args) {
String str = "xiaoLiGu";
int index = str.indexOf("G");
if (index!=-1){
System.out.println("包含");
}else {
System.out.println("不包含");
}
}
}
Contains()
用于判断字符串中是否包含指定的字符或字符串
语法:
public boolean contains(CharSequence chars)//chars:需要判断的字符串
实例:
public class Test03 {
public static void main(String[] args) {
String str ="zhangSan";
//true
System.out.println(str.contains("S"));
//false
System.out.println(str.contains("s"));
}
}
版权声明:本文为qq_71314862原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。