public class StringDemo {
public static void main(String[] args) {
//判断
String s="abcde";
System.out.println("startsWith(String str):"+s.startsWith("ab"));//true
System.out.println("endsWith(String str):"+s.endsWith("e"));//true
System.out.println("contains(String str):"+s.contains("cd"));//true
System.out.println("isEmpty():"+s.isEmpty());//false
System.out.println("equals(String str):"+s.equals("AbCde"));//false,区分大小写
System.out.println("equalsIgnoreCase(String str):"+s.equalsIgnoreCase("AbCde"));//true,不区分大小写
//获取
String str="abcddfefd";
System.out.println("str.length():"+str.length());//9
System.out.println("str.indexOf(int ch):"+str.indexOf('f'));//5
System.out.println("str.indexOf(String str):"+str.indexOf("d"));//3
System.out.println("str.lastIndexOf(int ch):"+str.lastIndexOf('f'));//7
System.out.println("str.lastIndexOf(String str):"+str.lastIndexOf("d"));//8
System.out.println("str.charAt(int index):"+str.charAt(0));//a
System.out.println("str.substring(int beginIndex):"+str.substring(2));//cddfefd
System.out.println("str.substring(int beginIndex,int endIndex):"+str.substring(3, 5));//dd
//转换
String str1="HelloWorld";
byte[] byt=str1.getBytes();
for (int i = 0; i < byt.length; i++) {
System.out.print(byt[i]+" ");//72 101 108 108 111 87 111 114 108 100
}
System.out.println();
char[] chs=str1.toCharArray();
for (int i = 0; i < chs.length; i++) {
System.out.print(chs[i]+" ");// H e l l o W o r l d
}
System.out.println();
System.out.println("String.valueOf(int i)基本类型:"+String.valueOf(123));//123
char[] ch={'a','b','c'};
System.out.println("String.valueOf(char[] data):"+String.valueOf(ch));//abc
System.out.println("str1.toUpperCase():"+str1.toUpperCase());//HELLOWORLD
System.out.println("str1.toLowerCase():"+str1.toLowerCase());//helloworld
//替换
String str2=" hello----world ";
System.out.println("str2.replace(char old.char new):"+str2.replace('o', 'O'));// hellO----wOrld
System.out.println("str2.trim():"+str2.trim());//hello----world
String[] strs=str2.trim().split("-");
for (int i = 0; i < strs.length; i++) {
System.out.print(strs[i]+" ,");//hello, , , ,world,
}
System.out.println();
System.out.println(strs.length);//5
}
} |