1.public char charAt(int index)返回字符串中第index个字符;
String s1 = "Hello World"
System.out.println(s1.charAt(4)) // 输出o
2.public int length()返回字符串的长度;
System.out.println(s1.length) // 输出11
3.public String[] split(String regex)将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
String date = "2008/09/10"; String[ ] dateAfterSplit= new String[3]; dateAfterSplit=date.split("/"); //以“/”作为分隔符来分割date字符串,并把结果放入3个字符串中。 for(int i=0;i<dateAfterSplit.length;i++) System.out.print(dateAfterSplit[i]+" "); }
输出结果:2008 09 10
4.public String substring(int beginIndex,int endIndex)返回该字符串从beginIndex开始到endsIndex结尾的子字符串
System.out.println(s1.substring(1,3)) // 输出ell字符串
5.public String replace(char oldchar,char newChar)在字符串中用newChar字符替换oldChar字符
System.out.println(s1.replace("o" ,"123")) // 输出Hell123 World字符串
6.int compareTo(String anotherString)
当前String对象与anotherString比较
String s2 = "HelloWorld"
String s3 = "Hello World"
String s4 = "HelloWorldabc"
System.out.println(s1.compareTo(s2)) // 输出5
System.out.println(s1.compareTo(s3)) // 输出0