.NET Framework 類別庫
StringBuilder 類別

表示可變動的字元字串。這個類別無法被繼承。

命名空間: System.Text
組件: mscorlib (在 mscorlib.dll 中)

語法

Visual Basic (宣告)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class StringBuilder
    Implements ISerializable
Visual Basic (使用方式)
Dim instance As StringBuilder
C#
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class StringBuilder : ISerializable
C++
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class StringBuilder sealed : ISerializable
J#
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class StringBuilder implements ISerializable
JScript
SerializableAttribute 
ComVisibleAttribute(true) 
public final class StringBuilder implements ISerializable
備註

這個類別表示一個和字串相似的物件,它的值是一個可變動的連續字元。值之所以可變動的原因是一旦以附加、移除、取代或插入字元的方式建立後就可以修改。如需詳細資訊,請參閱 String 類別。

修改這個類別其執行個體的方法大都會傳回對相同執行個體的參考。因為已傳回對執行個體的參考,所以您可以呼叫參考上的方法或屬性。當您想撰寫可以依序鏈結連續作業的單一陳述式時,這種方式就非常適用。

StringBuilder 的容量是執行個體在任何指定時間都可以儲存的最大字元數,並且大於或等於執行個體值的字串表示長度。這個容量可以利用 Capacity 屬性或 EnsureCapacity 方法增加或減少,但它不可以少於 Length 屬性的值。

初始化 StringBuilder 執行個體時如果沒有指定容量或最大容量,則使用實作特有的預設值。

效能考量

ConcatAppendFormat 方法都會將新的資料串連到現有的 StringStringBuilder 物件。String 物件串連作業永遠都會從現有的字串和新資料建立新的物件。StringBuilder 物件會維護一個緩衝區,以容納新資料的串連。如果有可用的空間,新的資料會附加至緩衝區的尾端,否則,會配置較大的新緩衝區,而原始緩衝區的資料會複製到新的緩衝區,然後新的資料會附加至新的緩衝區。

StringStringBuilder 物件之串連作業的效能是根據記憶體的配置頻率而定。String 串連作業永遠都會配置記憶體,而 StringBuilder 串連作業只有在 StringBuilder 物件緩衝區太小而無法容納新資料時,才會配置記憶體。因此,如果要串連固定數目的 String 物件,最好使用 String 類別的串連作業。在這種情況下,編譯器 (Compiler) 甚至可能將個別的串連作業結合成一個單一作業。如果要串連任意數目的字串 (例如,如果迴圈串連任意數目的使用者輸入字串),則對於串連作業來說最好使用 StringBuilder 物件。

實作者注意事項 這項實作的預設容量是 16,而最大的容量是 Int32.MaxValue。 當容量依照執行個體的值增加時,StringBuilder 可以配置更多儲存字元所需的記憶體。配置的記憶體數量是實作特有的,如果需要的記憶體數量超過最大容量,則擲回 ArgumentOutOfRangeException。 例如,AppendAppendFormatEnsureCapacityInsertReplace 方法可以放大執行個體值。 Chars 屬性可以存取 StringBuilder 值的個別字元。從零起始的索引位置。

範例

下列程式碼範例會示範,如何呼叫許多由 StringBuilder 類別所定義的方法。

C#
using System;
using System.Text;

public sealed class App 
{
    static void Main() 
    {
        // Create a StringBuilder that expects to hold 50 characters.
        // Initialize the StringBuilder with "ABC".
        StringBuilder sb = new StringBuilder("ABC", 50);

        // Append three characters (D, E, and F) to the end of the StringBuilder.
        sb.Append(new char[] { 'D', 'E', 'F' });

        // Append a format string to the end of the StringBuilder.
        sb.AppendFormat("GHI{0}{1}", 'J', 'k');

        // Display the number of characters in the StringBuilder and its string.
        Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());

        // Insert a string at the beginning of the StringBuilder.
        sb.Insert(0, "Alphabet: ");

        // Replace all lowercase k's with uppercase K's.
        sb.Replace('k', 'K');

        // Display the number of characters in the StringBuilder and its string.
        Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());
    }
}

// This code produces the following output.
//
// 11 chars: ABCDEFGHIJk
// 21 chars: Alphabet: ABCDEFGHIJK
C++
using namespace System;
using namespace System::Text;

int main()
{
    // Create a StringBuilder that expects to hold 50 characters.
    // Initialize the StringBuilder with "ABC".
    StringBuilder^ sb = gcnew StringBuilder("ABC", 50);

    // Append three characters (D, E, and F) to the end of the
    // StringBuilder.
    sb->Append(gcnew array<Char>{'D', 'E', 'F'});

    // Append a format string to the end of the StringBuilder.
    sb->AppendFormat("GHI{0}{1}", (Char)'J', (Char)'k');

    // Display the number of characters in the StringBuilder
    // and its string.
    Console::WriteLine("{0} chars: {1}", sb->Length, sb->ToString());

    // Insert a string at the beginning of the StringBuilder.
    sb->Insert(0, "Alphabet: ");

    // Replace all lowercase k's with uppercase K's.
    sb->Replace('k', 'K');

    // Display the number of characters in the StringBuilder
    // and its string.
    Console::WriteLine("{0} chars: {1}", sb->Length, sb->ToString());
}

// This code produces the following output.
//
// 11 chars: ABCDEFGHIJk
// 21 chars: Alphabet: ABCDEFGHIJK
繼承階層架構

System.Object
  System.Text.StringBuilder
執行緒安全

這個型別的所有公用靜態成員 (即 Visual Basic 中的 Shared 成員) 都是安全執行緒。並非所有的執行個體成員均為安全執行緒。
平台

Windows 98、 Windows 2000 SP4、 Windows CE、 Windows Millennium Edition、 Windows Mobile for Pocket PC、 Windows Mobile for Smartphone、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition

.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。

版本資訊

.NET Framework

支援版本:2.0、1.1、1.0

.NET Compact Framework

支援版本:2.0、1.0
請參閱

標記 :


Page view tracker