Java StringBuilder和StringBuffer用法

StringBuilderStringBufferString類的同伴類。它們表示一個可變的字符序列。StringBuffer是線程安全的,StringBuilder不是線程安全的。

兩個類都有相同的方法,除了StringBuffer中的所有方法都是同步的。StringBuilder對象是可修改的字符串。StringBuilder類包含四個構造函數:

StringBuilder()
StringBuilder(CharSequence seq)
StringBuilder(int capacity)
StringBuilder(String str)

無參數構造函數創建一個默認容量爲16的空StringBuilder對象。第二個構造函數使用CharSequence對象作爲參數。它創建一個StringBuilder對象,其內容與指定的CharSequence相同。

第三個構造函數使用int作爲參數; 它創建一個空的StringBuilder對象,其初始容量與指定的參數相同。

以下是創建StringBuilder對象的一些示例:

StringBuilder sb1  = new StringBuilder();
StringBuilder sb2  = new StringBuilder("Here is  the   content");
StringBuilder sb3  = new StringBuilder(200);

append()方法將文本添加到StringBuilder的結尾處。它可使用多種類型的參數。insert()delete()用於修改字符串的內容。

長度和容量

StringBuilder類有兩個屬性:lengthcapacity。它的長度是指其內容的長度,而其容量是指它可以容納而不分配新的內存的最大字符數。length()capacity()方法分別返回其長度和容量。例如,

public class Main {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder(200); // Capacity:200, length:0
    sb.append("Hello"); // Capacity:200, length:5
    int len = sb.length(); // len is assigned 5
    int capacity = sb.capacity(); // capacity is assigned 200

  }
}

轉換爲字符串

可以通過使用toString()方法將StringBuilder的內容作爲String類型的字符串值。

public class Main {
  public static void main(String[] args) {
    // Create a String object
    String s1 = new String("Hello");
    // Create a StringBuilder from of the String object s1
    StringBuilder sb = new StringBuilder(s1);

    // Append " Java" to the StringBuilder's content
    sb.append(" Java"); // Now, sb contains "Hello Java"

    // Get a String from the StringBuilder
    String s2 = sb.toString(); // s2 contains "Hello Java"

  }
}

StringBuilder有一個setLength()方法,它的新長度作爲參數。如果新長度大於舊長度,則額外位置(多過的部分)用空字符填充(空字符爲\u0000)。

如果新長度小於舊長度,則其內容將被截斷以適應新長度。

public class Main {
  public static void main(String[] args) {
    // Length is 5
    StringBuilder sb = new StringBuilder("Hello");

    // Now the length is 7 with last two characters as null character '\u0000'
    sb.setLength(7);

    // Now the length is 2 and the content is "He"
    sb.setLength(2);

  }
}

示例

StringBuilder類有一個reverse()方法,它用相同的字符序列替換其內容,但順序相反。以下代碼顯示了StringBuilder類的一些方法的使用。

public class Main {
  public static void main(String[] args) {
    // Create an empty StringBuffer
    StringBuilder sb = new StringBuilder();
    printDetails(sb);

    // Append "good"
    sb.append("good");
    printDetails(sb);

    // Insert "Hi " in the beginning
    sb.insert(0, "Hi ");
    printDetails(sb);

    // Delete the first o
    sb.deleteCharAt(1);
    printDetails(sb);

    // Append "  be  with  you"
    sb.append(" be  with  you");
    printDetails(sb);

    // Set the length to 3
    sb.setLength(3);
    printDetails(sb);

    // Reverse the content
    sb.reverse();
    printDetails(sb);
  }

  public static void printDetails(StringBuilder sb) {
    System.out.println("Content: \"" + sb + "\"");
    System.out.println("Length: " + sb.length());
    System.out.println("Capacity: " + sb.capacity());

    // Print an empty line to separate results
    System.out.println();
  }
}

上面的代碼生成以下結果。

Content: ""
Length: 0
Capacity: 16

Content: "good"
Length: 4
Capacity: 16

Content: "Hi good"
Length: 7
Capacity: 16

Content: "H good"
Length: 6
Capacity: 16

Content: "H good be  with  you"
Length: 20
Capacity: 34

Content: "H g"
Length: 3
Capacity: 34

Content: "g H"
Length: 3
Capacity: 34

字符串連接運算符(+)

在開發過程中,也經常使用+運算符將字符串,原始類型值或對象連接成另一個字符串。

例如,

String str = "X" + "Y" + 12.56;

爲了優化字符串連接操作,編譯器用一個使用StringBuilder的語句替換字符串連接。

String str = new StringBuilder().append("X").append("Y").append(12.56).toString();