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值// 按照字典順序,比指定字串大,返回正數,小則返回負數,相等返回0int 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 獲得指定字串第一次出現的位置,沒有找到返回 -1System.out.println("(indexOf) : " + str.indexOf("cd"));// lastIndexOf 獲得指定字串最後一次出現的位置,沒有找到返回 -1System.out.println("(lastIndexOf) : " + str.lastIndexOf("de"));// split 根據指定的Regex,切割字串,返回字串數組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);
運行結果
StringBuffer 和 StringBuilder 具有相同的方法,區別在於 StringBuffer 是 安全執行緒的,而 StringBuilder 是 線程不安全的。