zoukankan      html  css  js  c++  java
  • 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);//HekkoWorld
    13         
    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,等于-1
    34         
    35         System.out.println(a1.compareTo(a3));//32
    36         //a1的c在ASCII的数值减去a2的C在ASCII的数值。得到32
    37         
    38         System.out.println(a2.compareTo(a4));//-8
    39         //a2的前2个字母do和a4前2个一样,a2第三个的g减去a4第三个的o,得到-8
    40         
    41         System.out.println(a1.compareTo(a5));//0
    42         //a1和a5的数组元素完全一样
    43         
    44         System.out.println(a1.compareTo(a6));//2
    45         //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 == 1
    66         
    67         int lim = Math.min(len1, len2);
    68         // lim = Math.min = 3-1 = 2; lim = 2
    69         
    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 = 2
    86     }
    87     
    88     String a1 = "cat";
    89     String a6 = "c";
    90     System.out.println(a1.compareTo(a6));//2
    何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
  • 相关阅读:
    DNS智能双向解析怎么做?
    高防服务器如何选择性价比最高?
    20192416 实验四《Python程序设计》综合实践报告
    20192416实验三 《Python程序设计》实验报告
    20192416 《Python程序设计》实验二报告
    20192416 《Python程序设计》实验一报告
    2019-2020-1学期 20192416《网络空间安全专业导论》第十二周学习总结
    2019-2020-1学期 20192416《网络空间安全专业导论》第十一周学习总结
    2019-2020-1学期 20192416《网络空间安全专业导论》第十周学习总结
    2019-2020-1学期 20192416《网络空间安全专业导论》第九周学习总结
  • 原文地址:https://www.cnblogs.com/LZL-student/p/5875711.html
Copyright © 2011-2022 走看看