1. 程式人生 > 實用技巧 >UPC——問題 E: 回憶與困惑

UPC——問題 E: 回憶與困惑

String類

String類基礎

  1. 代表字串,使用一對“”引起來表示
  2. 宣告為final類,不可被繼承
  3. String實現的介面
    • Serializable介面:表示字串是支援序列化的
    • Comparable介面:表示String可以比較大小
  4. String內部定義了final char[] value用於儲存字串資料
  5. String代表一個不可變的字元序列(不可變性)
    • 通過字面量的方式(區別於new方式)給一個字串複製,此時的字串值宣告在字串常量池中
    • 字串常量池中是不會儲存相同內容的字串的
    • 體現
      • 當對字串重新賦值時,需要重新指定記憶體區域賦值,不能使用原有的value進行賦值
      • 當對現有的字串進行連線操作時,也需要重新指定記憶體區域賦值,不能在原有的value進行賦值
      • 當呼叫String.replace()方法修改指定的字元或字串時,也必須重新指定記憶體區域賦值
  6. String例項化方式
    • 通過字面量定義的方式
    • 通過new + 構造器的方式
      • 變數儲存的是地址值,是資料在堆空間中開闢空間以後對應的地址值
  7. String字串拼接
    • 常量與常量的拼接結果在常量池,且常量池中不會存在相同內容的常量
    • 只要有一個是變數,結果就在堆中
    • ⭕如果拼接的結果呼叫intern()方法,返回值就在常量池中

String類常用方法

  1. s.length():返回字串的長度

    String s = "abcdefg";//7
    System.out.println(s.length());
    
  2. s.charAt(int index) :返回某索引處的字元

    index屬於陣列長度範圍

    String s = "abcdefg";
    System.out.println(s.charAt(3));//d
    
  3. s.isEmpty():判斷是否是空字串

    String s = "abcdefg";
    System.out.println(s.isEmpty());//false
    
  4. s.toLowerCase():將String中的所有字元轉換為小寫

    String s = "ABCDEFG";
    System.out.println(s.toLowerCase());//abcdefg
    
  5. s.toUpperCase():將String中的所有字元轉換為大寫

    String s = "abcdefg";
    System.out.println(s.toUpperCase());//ABCDEFG
    
  6. s.trim():返回字串副本,忽略前導空白和尾部空白

    String s = "  ASDF GJ  ";
    System.out.println(s.trim());//ASDF GJ
    
  7. equals():比較字串的內容是否相等

  8. s1.equalsIgnoreCase(s2):忽略大小寫比較兩者是否相等

    String s1 = "asdfg";
    String s2 = "ASDFG";
    System.out.println(s1.equalsIgnoreCase(s2));//true
    
  9. s1.concat("s2"):將指定字串連線到此字串的結尾,相當於用“+”

    String s1 = "qwert";
    System.out.println(s1.concat("+mnb"));//qwert+mnb
    
  10. s1.compareTo(s2):比較兩個字串的大小(無論哪種方式建的字串都可以)

    • 涉及到字串排序
    • 結果為負數代表前者小
    • 結果為0代表兩者相同
    String s1 = "asd";
    String s2 = "cvb";
    System.out.println(s1.compareTo(s2));//-2
    
  11. String s2 = s16.substring(int index):返回一個新的字串,它是此字串從index開始擷取到最後一個字串

    String s1 = "克里斯馬殷閣下";
    System.out.println(s1.substring(4));//殷閣下
    
  12. String s2 = s1.substring(int beginIndex, int endIndex):返回一個新的字串,它是從beginIndex開始擷取到endIndex(不包含)的一個子字串(左閉右開)

    String s1 = "克里斯馬殷閣下";
    System.out.println(s1.substring(0,4));//克里斯馬
    
  13. boolean b = s1.endsWith(String suffix) :測試此字串是否以指定的字尾結束

    String s = "asdfg";
    System.out.println(s.endsWith("g"));//true
    
  14. boolean b = s1.startsWith(String suffix):測試此字串是否以指定的字首開始

    String s = "asdfg";
    System.out.println(s.startsWith("a"));//true
    
  15. boolean b = s.startsWith(prefix,toffset):測試此字串從指定的索引開始的子字串是否與指定字首開始

    String s = "asdfg";
    System.out.println(s.startsWith("df", 2));//true
    
  16. s1.contains(s2):此字串包含指定的char值序列時,返回true

    String s = "asdfg";
        System.out.println(s.contains("sdf"));//true
        System.out.println(s.contains("sf"));//false
    
  17. s.indexOf(String str):返回指定子字串中第一次出現處的索引,不存在出現-1

    String s = "asdfg";
    System.out.println(s.indexOf("d"));//2
    System.out.println(s.indexOf("b"));//-1
    
  18. s.indexOf(str,fromIndex):返回指定子字串在此字串中第一次出現的索引,從fromIndex開始查詢

    String s = "asdfgasdfg";
    System.out.println(s.indexOf("d", 5));//7
    
  19. s.lastIndexOf(String str):返回指定的子字串在此字串中最右邊出現處的索引(還是從前向後找,返回的索引也是從前往後數的索引)

    String s = "asdfgasdfg";
    System.out.println(s.lastIndexOf("f"));//8
    
    • 什麼情況indexOf(str)和lastIndexOf(str)返回值相同?
      • str中的元素都只存在一個
      • 不存在str
  20. str.lastIndexOf(str,fromIndex):返回指定的子字串中最後一次出現處的索引,從指定的索引開始反向搜尋(返回的索引值還是從前往後找得到的索引)

    String s = "asdfgasdfg";
    System.out.println(s.lastIndexOf("g", 5));//4
    
  21. s.replace(oldchar,newchar):返回一個新的字串,用newchar替換oldchar(替換字元)

    String s = "asdfgasdfg";
    String replace = s.replace("as", "bn");
    System.out.println(replace);//bndfgbndfg
    
  22. s.replace(target, replacement):返回一個新的字串,replacement替換target

    String s = "最好的隊長殷志源";
    String replace = s.replace("最好的隊長", "丸子");
    System.out.println(replace);//丸子殷志源