[Java] 纯文本查看 复制代码
//1 charAt 根据索引返回该位置的字符
String s = "jixiaomeng";
char c = s.charAt(6);
System.out.println(c);//m
//2 contains 传入字符串 判断是否包含该字符串
boolean b = s.contains("jixiaomeng");
System.out.println(b);//true
//3 equals 是否相等 equalsIgnoreCase
boolean b2 = s.equals("xiao");
System.out.println(b2);//false
//4 boolean endsWith(String suffix) startWith
boolean b3 = s.endsWith("g");//jixiaomeng
System.out.println(b3);//true
//5 int indexOf(int ch) 小串再大串中第一次出现的索引
//int lastIndexOf(String str)
String s5 = "hwheimaloWorheimald";
int i = s5.indexOf("heima");//2
System.out.println(i);//2
int i2 = s5.lastIndexOf("heima");
System.out.println("i2= "+i2);//12
//6 isEmpty 判断是否为空
boolean b4 = s5.isEmpty();
System.out.println("b4= "+b4);//false
//7 length()长度
int i3 = s5.length();
//8,切割:split 返回的是字符串数组
String s7 = "86-136 202 030 30";
//{"86","13620203030"}
String[] srr = s7.split(" ");
for(int x=0;x<srr.length;x++){
System.out.println(srr[x]);
}
//9 截取:subString 截取一个字符串返回新的字符串,不影响之前的
String s8 = "hwheimaloWorheimald";
String s9 = "heima";
int index = s8.indexOf(s9);//heima 的h第一次出现的索引
int index2 = index+s9.length();
System.out.println(index2);
String s10 = s8.substring(index2);// 一个参数的方法:截取到末尾
System.out.println(s10);
String s11 = s8.substring(7,11);//lowo 包含7 不包含11
System.out.println(s11);
//10 toUpperCase变成大写 toLowerCase变成小写
//11 trim 去除两端空格
//12 toCharArray 把字符串转换成字符类型的数组
char[] crr = s9.toCharArray();//heima
//{'h','e','i','m','a'}