JAVA的String類中常用的方法

@Test
    public void demo(){

        // 以下爲String中的常用的方法及註釋, 最常用的註釋前有**標註
        String s = "abcdefg123456";

        // 返回下標對應的ASCII碼
        int codePointAt = s.codePointAt(7);
        // **返回字符串s和字符串Q的,第一個不相等字符的ASCII碼差值,如果查找結束髮現都相等,則返回兩個字符串的長度差值
        int compareTo = s.compareTo("Q");
        // 返回下標前一位對應的ASCII碼
        int codePointBefore = s.codePointBefore(7);
        // 返回代碼點長度
        int codePointCount = s.codePointCount(4, 7);
        // 返回從index=4處偏移到codePointOffSet=7之後的索引
        int offsetByCodePoints = s.offsetByCodePoints(4, 7);
        // 將字符串變成字節數組
        byte[] getBytes = s.getBytes();
        // **將字符串變成字符數組
        char[] toCharArray = s.toCharArray();
        // **返回指定索引出的字符
        char charAt = s.charAt(7);
        // **是否包含字符串"contains"
        boolean contains = s.contains("contains");
        // **將字符串s中的c全部替換爲C
        String replaceAll = s.replaceAll("c", "C");
        // 將字符串中的1替換爲一
        String replace = s.replace("1", "一");
        // **截取字符串s中的索引從0到3的字符
        String substring = s.substring(0, 3);
        // 截取字符串s中索引從4開始的字符
        String substring1 = s.substring(4);
        // 字符串s是否以6結尾
        boolean endsWith = s.endsWith("6");
        // 字符串是否以a開頭
        boolean startsWith = s.startsWith("compareTo");
        // **將字符串s以字符c進行分割
        String[] split = s.split("c");
        // 將字符串s以字符c進行分割,分割成3份(如果最大能夠分割到3份的話,如果最大不能分割到3份,則默認分割到最大份數)
        String[] split1 = s.split("c", 3);
        // 將字符串7890拼接到字符串s的後面,與+拼接字符串的區別是,+號左右的字符串都可以是null,而concat左右都不允許,否則會報空指針異常
        String concat = s.concat("7890");
        // 返回字符串是否相等,與equals區別,equals僅限於兩個String類型字符串進行比較,而contentEquals可以比較類型爲CharSequence的,
        // 而且如果比較的是StringBuffer還會加鎖
        boolean contentEquals = s.contentEquals("contentEquals");
        // 比較兩個字符串是否相等(忽略大小寫),
        boolean equalsIgnoreCase = s.equalsIgnoreCase("Abcdefg123456");
        // **比較兩個字符串是否相等(不忽略大小寫)
        boolean equals = s.equals("contentEquals");
        // 判斷字符串是否爲空
        boolean empty = s.isEmpty();
        // **獲取字符串的長度
        int length = s.length();
        // **返回指定字符在字符串s中的索引,如果找不到返回-1
        int indexOf = s.indexOf("一");
        // 將字符串全部轉換爲小寫
        String toLowerCase = s.toLowerCase();
        // 將字符串全部轉換爲大寫
        String toUpperCase = s.toUpperCase();
        // 去除字符串前後的空格
        String trim = "  abc   ".trim();
        // 將指定數量的字符串(s,7890,AAA)以固定格式(,)進行拼接
        String join = String.join(",",s, "7890", "AAA");



        System.out.println("codePointAt = " + codePointAt);
        System.out.println("compareTo = " + compareTo);
        System.out.println("codePointBefore = " + codePointBefore);
        System.out.println("codePointCount = " + codePointCount);
        System.out.println("offsetByCodePoints = " + offsetByCodePoints);
        System.out.println("getBytes = " + Arrays.toString(getBytes));
        System.out.println("toCharArray = " + Arrays.toString(toCharArray));
        System.out.println("charAt = " + charAt);
        System.out.println("contains = " + contains);
        System.out.println("replaceAll = " + replaceAll);
        System.out.println("replace = " + replace);
        System.out.println("substring = " + substring);
        System.out.println("substring1 = " + substring1);
        System.out.println("endsWith = " + endsWith);
        System.out.println("startsWith = " + startsWith);
        System.out.println("split = " + Arrays.toString(split));
        System.out.println("split1 = " + Arrays.toString(split1));
        System.out.println("concat = " + concat);
        System.out.println("contentEquals = " + contentEquals);
        System.out.println("equalsIgnoreCase = " + equalsIgnoreCase);
        System.out.println("indexOf = " + indexOf);
        System.out.println("toLowerCase = " + toLowerCase);
        System.out.println("toUpperCase = " + toUpperCase);
        System.out.println("join = " + join);
        System.out.println("equals = " + equals);
        System.out.println("empty = " + empty);
        System.out.println("length = " + length);
        System.out.println("trim = " + trim);
    }

上述打印結果爲:

codePointAt = 49
compareTo = 16
codePointBefore = 103
codePointCount = 3
offsetByCodePoints = 11
getBytes = [97, 98, 99, 100, 101, 102, 103, 49, 50, 51, 52, 53, 54]
toCharArray = [a, b, c, d, e, f, g, 1, 2, 3, 4, 5, 6]
charAt = 1
contains = false
replaceAll = abCdefg123456
replace = abcdefg一23456
substring = abc
substring1 = efg123456
endsWith = true
startsWith = false
split = [ab, defg123456]
split1 = [ab, defg123456]
concat = abcdefg1234567890
contentEquals = false
equalsIgnoreCase = true
indexOf = -1
toLowerCase = abcdefg123456
toUpperCase = ABCDEFG123456
join = abcdefg123456,7890,AAA
equals = false
empty = false
length = 13
trim = abc

以上爲個人總結記錄使用,希望能夠幫助其他人,如有不足或錯誤歡迎大家指正。

 

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