startsWith
String str = "Hello World";
//返回值为true或false
boolean flag1 = str.startsWith("World");//从头查找
boolean flag2 = str.startsWith("pink",3);//从指定索引值开始往后查找
System.out.println(flag1);
System.out.println(flag2);
contains
String str = "Welcome to the beautiful world!";
//返回值为true或false
boolean flag = str.contains("beau");
System.out.println(flag);
//直接用在if语句中
if (str.contains("beautiful")) {
System.out.println("yes");
}
indexOf
String str = "1-2-3-4-5"
int index1 = str.indexOf("3");//从头开始检索
int index2 = str.indexOf("3",2);//从索引值为2的地方开始检索(索引值从0开始)
System.out.println(index1);
System.out.println(index2);
substring
String str = ""Welcome to the beautiful world!";
int index = str.indexOf("beautiful");//找到beautiful首次出现的位置
System.out.println(str.substring(index));//截取包含beautiful在内的之后的字符串
原文:https://www.cnblogs.com/xzdx/p/15187558.html