Java 字符串和字符串緩衝區的常用方法

Java 字符串和字符串緩衝區的常用方法

字符串 String

  • 具有判斷功能的方法

    • 示例

      // equals 判斷兩個字符串是否具有相同的字符序列
      String str = "abcdef";
      
      boolean result1 = str.equals("abcdef");
      boolean result2 = str.equals("ABCdef");
      
      System.out.println("(equals) : result1 = " + result1 + ", result2 = "
              + result2);
      
      // equalsIgnoreCase 與equals相同,但忽略大小寫
      result1 = str.equalsIgnoreCase("abcdef");
      result2 = str.equalsIgnoreCase("ABCdef");
      
      System.out.println("(equalsIgnoreCase) : result1 = " + result1
              + ", result2 = " + result2);
      
      // 判斷是否包含指定的字符串
      result1 = str.contains("a");
      result2 = str.contains("x");
      System.out.println("(contains) : result1 = " + result1 + ", result2 = "
              + result2);
      
      // startsWith 判斷是否以指定字符串開始
      result1 = str.startsWith("abc");
      result2 = str.startsWith("def");
      System.out.println("(startsWith) : result1 = " + result1
              + ", result2 = " + result2);
      
      // endsWith 判斷是否以指定字符串結束
      result1 = str.endsWith("abc");
      result2 = str.endsWith("def");
      System.out.println("(endsWith) : result1 = " + result1 + ", result2 = "
              + result2);
      
      // isEmpty 判斷字符串是否爲空
      result1 = str.isEmpty();
      result2 = "".isEmpty();
      System.out.println("(isEmpty) : result1 = " + result1 + ", result2 = "
              + result2);
      
      // compareTo 根據字典順序,判斷兩個字符串每個字符的Unicode值
      // 按照字典順序,比指定字符串大,返回正數,小則返回負數,相等返回0
      int num1 = str.compareTo("abcdef");
      int num2 = str.compareTo("abcdee");
      int num3 = str.compareTo("abcdeg");
      System.out.println("(compareTo) : num1 = " + num1 + ", num2 = " + num2
              + ", num3 = " + num3);
      
      // compareToIgnoreCase 忽略大小寫,與compareTo相同
      num1 = str.compareToIgnoreCase("ABCdef");
      System.out.println("(compareToIgnoreCase) : num1 = " + num1);
    • 運行結果
      運行結果

  • 具有獲取功能的方法

    • 演示

      // length 獲得字符串長度
      String str = "abcdef";
      
      System.out.println("(length) : " + str.length());
      
      // charAt 獲得指定位置的字符
      System.out.println("(charAt) : " + str.charAt(2));
      
      // indexOf 獲得指定字符串第一次出現的位置,沒有找到返回 -1
      System.out.println("(indexOf) : " + str.indexOf("cd"));
      
      // lastIndexOf 獲得指定字符串最後一次出現的位置,沒有找到返回 -1
      System.out.println("(lastIndexOf) : " + str.lastIndexOf("de"));
      
      // split 根據指定的正則表達式,切割字符串,返回字符串數組
      String[] ss = "abc|def|ghi".split("[|]");
      System.out.println("(split) : " + Arrays.toString(ss));
      
      // substring 截取字符串
      System.out.println("(substring) : " + str.substring(2)); // 從索引2的位置截取,截取到最後
      System.out.println("(substring) : " + str.substring(2, 4));// 從索引2的位置截取,截取到索引4,不包括4(包頭不包尾)
    • 運行結果
      這裏寫圖片描述

  • 具有轉換功能的方法

    • 示例

      // getBytes 將字符串轉換成字節數組
      String str = "abcdef";
      byte[] byteBuf = str.getBytes();
      System.out.println("(getBytes) : " + Arrays.toString(byteBuf));
      
      // toCharArray 將字符串轉換成字符數組
      char[] charBuf = str.toCharArray();
      System.out.println("(toCharArray) : " + Arrays.toString(charBuf));
      
      // valueOf 將任意值/對象轉換成字符串
      System.out.println("(valueOf) : "
              + String.valueOf(new char[] { 97, 98, 99 }));
      
      // toUpperCase 將字符轉換成大寫形式
      System.out.println("(toUpperCase) : " + str.toUpperCase());
      
      // toLowerCase 將字符串轉換成小寫形式
      System.out.println("(toLowerCase) : " + "ABCdef".toLowerCase());
      
      // concat 拼接兩個字符串
      str = "aaa".concat("ccc");
      System.out.println("(concat) : " + str);
    • 運行結果
      運行結果

  • 具有替換功能的方法

    • 示例

      // replace 把字符串中指定字符替換成新的字符串
      String str = " \t  abc abc abc   ";
      System.out.println("(replace) : " + str.replace("a", "x"));
      
      // trim 去除字符串兩端的空白符
      System.out.println("(trim) : " + str.trim());
    • 運行結果
      運行結果

字符串緩衝區 StringBuffer/StringBuilder

  • 常用方法

    • 示例

      StringBuffer sb = new StringBuffer();
      
      // 獲得 StringBuffer 緩衝區的初始長度
      System.out.println("(capacity) : " + sb.capacity());
      
      // append 追加字符串
      sb.append("abc");
      System.out.println("(append) : " + sb);
      
      // length 獲得緩衝區的實際長度
      System.out.println("(length) : " + sb.length());
      
      // insert 插入數據到緩衝區指定的位置
      sb.insert(1, "xx");
      System.out.println("(insert) : " + sb);
      
      // deleteCharAt 刪除指定位置的字符
      sb.deleteCharAt(2);
      System.out.println("(deleteCharAt) : " + sb);
      
      // delete 刪除指定範圍的字符
      sb.delete(2, sb.length());
      System.out.println("(delete) : " + sb);
      
      // replace 替換緩衝區指定範圍的字符串
      sb.replace(1, 2, "b");
      System.out.println("(replace) : " + sb);
      
      // reverse 反轉緩衝區內的字符串
      sb.reverse();
      System.out.println("(reverse) : " + sb);
      
      // substring 截取字符串,不會操作原緩衝區,返回一個新的字符串
      String str = sb.substring(1, 2);
      System.out.println("(substring) : sb = " + sb + ", str = " + str);
    • 運行結果
      運行結果

  • StringBufferStringBuilder 具有相同的方法,區別在於 StringBuffer線程安全的,而 StringBuilder線程不安全的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章