Java中表示字元串的有三個類:String、StringBuffer和StringBuilder。其中,String的長度是不可變的,而StringBuffer和StringBuilder是長度可變的。對于StringBuffer和StringBuilder來說,兩者的API幾乎一模一樣,因為兩者有共同的父類:AbstractStringBuilder。差別在于StringBuffer的大部分方法都是被synchronized關鍵字修飾,是線程安全的,而StringBuilder則與其相反。
是以本篇就對StringBuffer和StringBuilder類比着來進行介紹。
- 構造器
StringBuffer和StringBuilder的構造器都有三個,分别如下:
StringBuffer的構造器:
- new StringBuffer(int capacity)
- new StringBuffer(String str)
- new StringBuffer(CharSequence seq)
StringBuilder的構造器:
- new StringBuilder(int capacity)
- new StringBuilder(String str)
- new StringBuilder(CharSequence seq)
對比兩者構造器發現,它們對構造器的重載都施行了同樣的方式,可以傳入一個整數的容量,可以傳一個字元串值,甚至可以傳一個字元序列的對象。對于Java來說,常用的字元序列對象就是我們認知中的String, StringBuffer和StringBuilder。關于StringBuilder和StringBuffer的使用的示例代碼如下所示:
public class StringTest { public static void main(String[] args) throws IOException { // 初始化一個容量為16,但沒有值的空StringBuffer對象 StringBuffer strBuffer = new StringBuffer(16); System.out.println("strBuffer:" + strBuffer + ", 容量為:" + strBuffer.capacity() + ", 長度為:" + strBuffer.length()); // 初始化一個容量為16,但沒有值的空StringBuilder對象 StringBuilder strBuilder = new StringBuilder(16); System.out.println("strBuilder:" + strBuilder + ", 容量為:" + strBuilder.capacity() + ", 長度為:" + strBuilder.length()); // 根據一個字元串初始化一個StringBuffer對象 StringBuffer strBuffer2 = new StringBuffer("how are you"); System.out.println("strBuffer2的值為:" + strBuffer2 + ", 容量為:" + strBuffer2.capacity() + ", 長度為:" + strBuffer2.length()); // 根據一個字元串初始化一個StringBuilder對象 StringBuilder strBuilder2 = new StringBuilder("how old are you"); System.out.println("strBuilder2的值為:" + strBuilder2 + ", 容量為:" + strBuilder2.capacity() + ", 長度為:" + strBuilder2.length()); // 根據一個字元序列對象如StringBuilder來初始化一個StringBuffer對象 StringBuilder strBuffer3 = new StringBuilder(strBuilder); System.out.println("strBuffer3的值為:" + strBuffer3 + ", 容量為:" + strBuffer3.capacity() + ", 長度為:" + strBuffer3.length()); // 根據一個字元序列對象如StringBuffer來初始化一個StringBuilder對象 StringBuilder strBuilder3 = new StringBuilder(strBuffer2); System.out.println("strBuilder3的值為:" + strBuilder3 + ", 容量為:" + strBuilder3.capacity() + ", 長度為:" + strBuilder3.length()); }}
執行結果如下圖所示:
- 拼接參數
拼接參數常用的方法方法如下圖:
也就是說,該方法可以拼接所有的基本資料類型和其對應的包裝類型,字元數組、字元序列對象及其他的引用對象等。
StringBuilder和StringBuffer的append
其示例代碼如下所示:
public class StringTest { public static void main(String[] args) throws IOException { // 可以拼接所有的基本資料類型 StringBuilder strBuilder = new StringBuilder(); StringBuffer strBuffer = new StringBuffer(); // 拼接int(byte、short都可以自動轉換為int) strBuilder.append(12).append(","); strBuffer.append(12).append(","); // 拼接long strBuilder.append(13L).append(","); strBuffer.append(13L).append(","); // 拼接float strBuilder.append(3.4f).append(","); strBuffer.append(3.4f).append(","); // 拼接double strBuilder.append(3.5).append(","); strBuffer.append(3.5).append(","); // 拼接字元數組 strBuilder.append("hello".toCharArray()).append(","); strBuffer.append("hello".toCharArray()).append(","); // 拼接其他引用對象 strBuilder.append(new Date()).append(","); strBuffer.append(new Date()).append(","); // 拼接指定字元數組偏移指定位數後的指定長度字元 strBuilder.append("hello".toCharArray(), 2, 2).append(","); strBuffer.append("hello".toCharArray(), 2, 2).append(","); // 拼接指定字元序列對象(常見的為String、StringBuffer和StringBuilder)指定開始和結束(不包括)的字元串 strBuilder.append("hello", 1, 3).append(","); strBuffer.append("hello", 1, 3).append(","); printStrBuilder(strBuilder); printStrBuffer(strBuffer); } private static void printStrBuilder(StringBuilder strBuilder) { String[] strArr = strBuilder.deleteCharAt(strBuilder.length() - 1).toString().split(","); System.out.println("StringBuilder資訊為:" + Arrays.asList(strArr)); } private static void printStrBuffer(StringBuffer strBuffer) { String[] strArr = strBuffer.deleteCharAt(strBuffer.length() - 1).toString().split(","); System.out.println("StringBuffer資訊為:" + Arrays.asList(strArr)); }}
隻想結果如下圖所示:
- 擷取某個字元串在另一個字元串中的索引位置
這裡使用的方法有四個,如下圖:
相關的示例代碼如下所示:
public class StringTest { public static void main(String[] args) throws IOException { StringBuilder strBuilder = new StringBuilder("no zuo no die no happy no problem"); StringBuffer strBuffer = new StringBuffer("no zuo no die no happy no problem"); // indexOf System.out.println(""no"在strBuilder中首次出現的位置為:" + strBuilder.indexOf("no")); System.out.println(""no"在strBuffer中首次出現的位置為:" + strBuffer.indexOf("no")); System.out.println(""no"在strBuilder中在索引3之後首次出現的位置為:" + strBuilder.indexOf("no", 3)); System.out.println(""no"在strBuffer中在索引3之後首次出現的位置為:" + strBuffer.indexOf("no", 3)); // lastIndexOf System.out.println(""no"在strBuilder中最後出現的位置為:" + strBuilder.lastIndexOf("no")); System.out.println(""no"在strBuffer中最後出現的位置為:" + strBuffer.lastIndexOf("no")); System.out.println(""no"在strBuilder中在索引20之前最後出現的位置為:" + strBuilder.lastIndexOf("no", 20)); System.out.println(""no"在strBuffer中在索引20之前最後出現的位置為:" + strBuffer.lastIndexOf("no", 20)); }}
執行結果如下圖所示:
- 插入方法
插入方法為:insert(int offset, XXX xxx), 目的是在偏移offset個字元後插入xxx。這裡的xxx表示所有的基本資料類型及其對應的包裝類型、字元數組、字元序列對象和其他的引用對象等。常用方法如下:
相關示例代碼如下所示:
public class StringTest { public static void main(String[] args) { StringBuilder strBuilder = new StringBuilder("hello"); StringBuffer strBuffer = new StringBuffer("hello"); System.out.println("在strBuilder偏移2位後插入整數3的結果為:" + strBuilder.insert(2, 3)); System.out.println("在strBuffer偏移3位後插入整數3的結果為:" + strBuffer.insert(3, 3)); StringBuilder strBuilder2 = new StringBuilder("world"); StringBuffer strBuffer2 = new StringBuffer("world"); System.out.println("在strBuilder2偏移2位後插入'好'的結果為:" + strBuilder2.insert(2, '好')); System.out.println("在strBuffer2偏移3位後插入'好'的結果為:" + strBuffer2.insert(3, '好')); StringBuilder strBuilder3 = new StringBuilder("what"); StringBuffer strBuffer3 = new StringBuffer("what"); System.out.println("在strBuilder3偏移2位後插入4L的結果為:" + strBuilder3.insert(2, 4L)); System.out.println("在strBuffer3偏移3位後插入4L的結果為:" + strBuffer3.insert(3, 4L)); StringBuilder strBuilder4 = new StringBuilder("where"); StringBuffer strBuffer4 = new StringBuffer("where"); System.out.println("在strBuilder4偏移2位後插入3.14F的結果為:" + strBuilder4.insert(2, 3.14F)); System.out.println("在strBuffer4偏移3位後插入3.14F的結果為:" + strBuffer4.insert(3, 3.14F)); StringBuilder strBuilder5 = new StringBuilder("when"); StringBuffer strBuffer5 = new StringBuffer("when"); System.out.println("在strBuilder5偏移2位後插入1.414的結果為:" + strBuilder5.insert(2, 1.414)); System.out.println("在strBuffer5偏移3位後插入1.414的結果為:" + strBuffer5.insert(3, 1.414)); StringBuilder strBuilder6 = new StringBuilder("crazy"); StringBuffer strBuffer6 = new StringBuffer("crazy"); System.out.println("在strBuilder6偏移2位後插入true的結果為:" + strBuilder6.insert(2, true)); System.out.println("在strBuffer6偏移3位後插入false的結果為:" + strBuffer6.insert(3, false)); StringBuilder strBuilder7 = new StringBuilder("hehe"); StringBuffer strBuffer7 = new StringBuffer("hehe"); System.out.println("在strBuilder7偏移2位後插入Date的結果為:" + strBuilder7.insert(2, new Date())); System.out.println("在strBuffer7偏移3位後插入Date的結果為:" + strBuffer7.insert(3, new Date())); StringBuilder strBuilder8 = new StringBuilder("this"); StringBuffer strBuffer8 = new StringBuffer("this"); System.out.println("在strBuilder8偏移2位後插入字元數組['a', 'r', 'e']的結果為:" + strBuilder8.insert(2, new char[]{'a', 'r', 'e'})); System.out.println("在strBuffer8偏移3位後插入字元數組['a', 'r', 'e']的結果為:" + strBuffer8.insert(3, new char[]{'a', 'r', 'e'})); StringBuilder strBuilder9 = new StringBuilder("happend"); StringBuffer strBuffer9 = new StringBuffer("happend"); System.out.println("在strBuilder9偏移2位後插入Date的結果為:" + strBuilder9.insert(2, new StringBuffer("234"))); System.out.println("在strBuffer9偏移3位後插入Date的結果為:" + strBuffer9.insert(3, new StringBuilder("234"))); StringBuilder strBuilder10 = new StringBuilder("that"); StringBuffer strBuffer10 = new StringBuffer("that"); System.out.println("在strBuilder10偏移2位後插入字元數組['a', 'r', 'e']的結果為:" + strBuilder10.insert(2, new char[]{'a', 'r', 'e'}, 0, 2)); System.out.println("在strBuffer10偏移3位後插入字元數組['a', 'r', 'e']的結果為:" + strBuffer10.insert(3, new char[]{'a', 'r', 'e'}, 0, 2)); StringBuilder strBuilder11 = new StringBuilder("jerry"); StringBuffer strBuffer11 = new StringBuffer("jerry"); System.out.println("在strBuilder11偏移2位後插入Date的結果為:" + strBuilder11.insert(2, new StringBuffer("234"), 0, 2)); System.out.println("在strBuffer11偏移3位後插入Date的結果為:" + strBuffer11.insert(3, new StringBuilder("234"), 0, 2)); }}
執行結果如下圖所示:
- 删除某個或某些字元
删除某個或某些的字元方法如下圖所示:
相關示例代碼如下:
public class StringTest { public static void main(String[] args) { StringBuilder strBuilder = new StringBuilder("hello"); StringBuffer strBuffer = new StringBuffer("hello"); System.out.println("strBuilder删除索引為1處的字元後結果為:" + strBuilder.deleteCharAt(1)); System.out.println("strBuffer删除索引為1處的字元後結果為:" + strBuffer.deleteCharAt(1)); StringBuilder strBuilder2 = new StringBuilder("hello"); StringBuffer strBuffer2 = new StringBuffer("hello"); System.out.println("strBuilder2删除索引從1到3(不包括3)的字元後結果為:" + strBuilder2.delete(1, 3)); System.out.println("strBuffer2删除索引從1到3(不包括3)的字元後結果為:" + strBuffer2.delete(1, 3)); }}
執行結果如下圖所示:
- 取子字元串
取子字元串的方法如下:
示例代碼如下所示:
public class StringTest { public static void main(String[] args) { StringBuilder strBuilder = new StringBuilder("hello world"); StringBuffer strBuffer = new StringBuffer("hello world"); System.out.println("strBuilder從索引3處開始到字元串末尾的子字元串為:" + strBuilder.substring(3)); System.out.println("strBuffer從索引3處開始到字元串末尾的子字元串為:" + strBuffer.substring(3)); StringBuilder strBuilder2 = new StringBuilder("hello world"); StringBuffer strBuffer2 = new StringBuffer("hello world"); System.out.println("strBuilder從索引3處開始到5(不包括5)的子字元串為:" + strBuilder.substring(3, 5)); System.out.println("strBuffer從索引3處開始到5(不包括5)的子字元串為:" + strBuffer.substring(3, 5)); }}
執行結果如下圖所示:
- 其他
除了以上列舉的方法外,還有一些常用的方法,分别為:
- int capacity():擷取容量(初始化字元數組的長度)
- int length():擷取長度(實際字元的長度)
- replace(int start, int end, String str):将索引從start到end(不包括end)的字元序列替換為str
- char charAt(int index):擷取指定索引出的字元
- void setCharAt(int index, char ch):用ch替換指定索引處的字元
- void setLength(int newwLength):将字元序列強制變為指定長度,多餘的字元被置為null。
- CharSequence subSequence(int start, int end):擷取子字元序列對象。
- reverse():将字元序列進行反轉。
示例代碼如下所示:
public class StringTest { public static void main(String[] args) { StringBuilder strBuilder = new StringBuilder("hello world"); StringBuffer strBuffer = new StringBuffer("hello world"); // 擷取字元容量 System.out.println("strBuilder的容量為:" + strBuilder.capacity()); System.out.println("strBuffer的容量為:" + strBuffer.capacity()); // 擷取長度 System.out.println("strBuilder的長度為:" + strBuilder.length()); System.out.println("strBuffer的長度為:" + strBuffer.length()); // 替換字元串 System.out.println("用"tom"替換strBuilder的0到5(不包括5)後結果為:" + strBuilder.replace(0, 5, "tom")); System.out.println("用"tom"替換strBuffer的0到5(不包括5)後結果為:" + strBuffer.replace(0, 5, "tom")); // 擷取指定索引處的字元 System.out.println("strBuilder的索引為1的字元為:" + strBuilder.charAt(1)); System.out.println("strBuffer的索引為1的字元為:" + strBuffer.charAt(1)); // 将字元序列強制變為指定長度 strBuilder.setLength(3); System.out.println("strBuilder強制變為長度為3的結果為:" + strBuilder); strBuffer.setLength(3); System.out.println("strBuffer強制變為長度為3的結果為:" + strBuffer); // 取子字元序列 System.out.println("strBuilder的從0到2的子字元序列為:" + strBuilder.subSequence(0, 2)); System.out.println("strBuffer的從0到2的子字元序列為:" + strBuffer.subSequence(0, 2)); // 字元序列反轉 System.out.println("strBuilder的反轉後為:" + strBuilder.reverse()); System.out.println("strBuffer的反轉為:" + strBuffer.reverse()); }}
執行結果如下圖所示:
自此,關于StringBuffer和StringBuilder的介紹就已經算是結束了。