package cn.itcast.myString;
/*
* String的其他方法:
* 1:public String trim() 返回字符串的副本,忽略前导空白和尾部空白。
* 2:public int compareTo(String anotherString) 按字典顺序比较两个字符串。
*/
public class Demo10 {
public static void main(String[] args) {
//1:
String s = "i love java";
String s2 = " i love java";
String s3 = "i love java ";
String s4 = " i love java ";
System.out.println(s.trim());
System.out.println(s2.trim());
System.out.println(s3.trim());
System.out.println(s4.trim());
//2按字典顺序比较,public int compareTo(String anotherString) 按字典顺序比较两个字符串。
String s5 = "abc";
String s6 = "bbc";
String s7 = "cbc";
System.out.println(s5.compareTo(s5));
System.out.println(s5.compareTo(s6));
System.out.println(s6.compareTo(s5));
System.out.println(s5.compareTo(s7));
System.out.println(s7.compareTo(s5));
String s8 = "abc";
String s9 = "abcd";
String s10 = "abcdefg";
System.out.println(s8.compareTo(s8));
System.out.println(s8.compareTo(s9));
System.out.println(s9.compareTo(s8));
System.out.println(s8.compareTo(s10));
System.out.println(s10.compareTo(s8));
String s11 = "abcc";
String s12 = "zbc";
System.out.println(s11.compareTo(s12));
System.out.println(s12.compareTo(s11));
String s13 = "aac";
String s14 = "abc";
System.out.println(s13.compareTo(s14));
}
} |
|