首页 > 代码库 > java11-6 String类的其它功能

java11-6 String类的其它功能


String类的其他功能:

  替换功能:
    String replace(char old,char new)
    String replace(String old,String new)

  去除字符串两空格
    String trim()

  按字典顺序比较两个字符串
    int compareTo(String str) 区分大小写
    int compareToIgnoreCase(String str) 不区分大小写

 1 public class StringTest3 { 2  3     public static void main(String[] args) { 4         String s = "HelloWorld"; 5          6         // 字符替代:String replace(char old,char new) 7         String s1 = s.replace(‘o‘,‘k‘); 8         System.out.println(s1);//HellkWkrld 9         10         // 字符串替代:String replace(String old,String new)11         String s2 = s.replace("ll", "kk");12         System.out.println(s2);//HekkoWorld13         14         15         // 去除字符串两空格16         String s4 = " hello world  ";17         String s5 = s4.trim();18         System.out.println("s4:" + s4 + "---");//左右的空格还保留19         System.out.println("s5:" + s5 + "---");//左右的空格去除了20 21         22         //按字典顺序(ASCII码表)比较两个字符串:23         // int compareTo(String str)24         String a1 = "cat";25         String a2 = "dog";26         String a3 = "Cat";27         String a4 = "door";28         String a5 = "cat";29         String a6 = "c";30         31         int i1 = a1.compareTo(a2);32         System.out.println(i1);33         //-1 c在b的前面一位,ASCII码表,a1的c减去a2的b,等于-134         35         System.out.println(a1.compareTo(a3));//3236         //a1的c在ASCII的数值减去a2的C在ASCII的数值。得到3237         38         System.out.println(a2.compareTo(a4));//-839         //a2的前2个字母do和a4前2个一样,a2第三个的g减去a4第三个的o,得到-840         41         System.out.println(a1.compareTo(a5));//042         //a1和a5的数组元素完全一样43         44         System.out.println(a1.compareTo(a6));//245         //Ctrl+左键点击comperTo看源码46     }47 48 }49 50 compareTo的源码,以及“System.out.println(a1.compareTo(a6));//2”的分析51 52  private final char value[];53   54     字符串会自动转换为一个字符数组。55   56   57 public int compareTo(String anotherString) {58         // this - a1 - "cat"59         // anotherString - a2 - "c"60         61         int len1 = value.length; 62         //this.value.length = a1.toCharArray().length == 3 63        64         int len2 = anotherString.value.length;65         //anotherString.value.length = a2.toCharArray().length == 166         67         int lim = Math.min(len1, len2);68         // lim = Math.min = 3-1 = 2; lim = 269         70         char v1[] = value;71         // char v1[] = {‘c‘,‘a‘,‘t‘};72        73         char v2[] = anotherString.value;74         // char v2[] = {‘c‘};75         76         int k = 0;77         while (k < lim) {78             char c1 = v1[k]; //c1 = ‘c‘,‘a‘,‘t‘79             char c2 = v2[k]; //c2 = ‘c‘80             if (c1 != c2) {81                 return c1 - c2;82             }83             k++; //当k=1时,k !< lim ,lim = 1; 所以跳出循环84         }85         return len1 - len2; //跳出循环后,到这里,比较两个数组的长度  3-1 = 286     }87     88     String a1 = "cat";89     String a6 = "c";90     System.out.println(a1.compareTo(a6));//2

 

java11-6 String类的其它功能