StringBuilder 類別

定義

表示可變動的字元字串。 此類別無法獲得繼承。

public ref class StringBuilder sealed
public ref class StringBuilder sealed : System::Runtime::Serialization::ISerializable
public sealed class StringBuilder
public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
[System.Serializable]
public sealed class StringBuilder
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
type StringBuilder = class
type StringBuilder = class
    interface ISerializable
[<System.Serializable>]
type StringBuilder = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StringBuilder = class
    interface ISerializable
Public NotInheritable Class StringBuilder
Public NotInheritable Class StringBuilder
Implements ISerializable
繼承
StringBuilder
屬性
實作

範例

下列範例示範如何呼叫 類別所 StringBuilder 定義的許多方法。

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
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
open System.Text

// Create a StringBuilder that expects to hold 50 characters.
// Initialize the StringBuilder with "ABC".
let sb = StringBuilder("ABC", 50)

// Append three characters (D, E, and F) to the end of the StringBuilder.
sb.Append [| 'D'; 'E'; 'F' |] |> ignore

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

// Display the number of characters in the StringBuilder and its string.
printfn $"{sb.Length} chars: {sb}"

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

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

// Display the number of characters in the StringBuilder and its string.
printfn $"{sb.Length} chars: {sb}"

// This code produces the following output.
//
// 11 chars: ABCDEFGHIJk
// 21 chars: Alphabet: ABCDEFGHIJK
Imports System.Text

Public Module App 
    Public Sub Main() 
        ' Create a StringBuilder that expects to hold 50 characters.
        ' Initialize the StringBuilder with "ABC".
        Dim sb As New StringBuilder("ABC", 50)

        ' Append three characters (D, E, and F) to the end of the StringBuilder.
        sb.Append(New Char() {"D"c, "E"c, "F"c})

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

        ' 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())
    End Sub
End Module

' This code produces the following output.
'
' 11 chars: ABCDEFGHIJk
' 21 chars: Alphabet: ABCDEFGHIJK

備註

這個類別代表類似字串的物件,其值為可變動的字元序列。

本節內容:

String 和 StringBuilder 類型

雖然 StringBuilderString 都代表字元序列,但它們會以不同的方式實作。 String 是不可變的類型。 也就是說,每個看似修改 String 物件的作業實際上都會建立新的字串。

例如,下列 C# 範例中方法的 String.Concat 呼叫似乎變更名為 value 的字串變數值。 事實上, Concat 方法會 value 傳回物件,該物件與傳遞至 方法的物件具有不同的值和位址 value 。 請注意,此範例必須使用編譯器選項進行 /unsafe 編譯。

using System;

public class Example
{
   public unsafe static void Main()
   {
      string value = "This is the first sentence" + ".";
      fixed (char* start = value)
      {
         value = String.Concat(value, "This is the second sentence. ");
         fixed (char* current = value)
         {
            Console.WriteLine(start == current);
         }
      }   
   }
}
// The example displays the following output:
//      False
    let mutable value = "This is the first sentence" + "."
    use start = fixed value
    value <- System.String.Concat(value, "This is the second sentence. ")
    use current = fixed value
    printfn $"{start = current}"
// The example displays the following output:
//      False

對於執行大量字串操作的常式 (,例如在迴圈中修改字串多次的應用程式) ,重複修改字串可能會確實造成顯著的效能負面影響。 替代方法是使用 StringBuilder ,這是可變動的字串類別。 可變性表示一旦建立類別的實例,就可以藉由附加、移除、取代或插入字元來修改它。 StringBuilder物件會維護緩衝區,以容納字串的擴充。 如果有空間可用,則會將新資料附加至緩衝區;否則,會配置新的較大緩衝區、原始緩衝區中的資料複製到新的緩衝區,然後將新的資料附加至新的緩衝區。

重要

雖然類別 StringBuilder 通常提供比 String 類別更好的效能,但當您想要操作字串時,您不應該自動取代 StringStringBuilder 。 效能取決於字串的大小、要配置給新字串的記憶體數量、執行程式碼的系統,以及作業的類型。 您應該準備好測試程式碼,以判斷是否實際 StringBuilder 提供顯著的效能改善。

請考慮在下列情況下使用 類別 String

  • 當程式碼對字串所做的變更數目很小時。 在這些情況下, StringBuilder 可能會對 提供可忽略或沒有效能 String 改善。

  • 當您執行固定數目的串連作業時,特別是字串常值。 在此情況下,編譯器可能會將串連作業合併成單一作業。

  • 當您建置字串時,必須執行廣泛的搜尋作業時。 類別 StringBuilder 缺少 搜尋方法,例如 IndexOfStartsWith 。 您必須將 物件 String 轉換成這些作業的 StringBuilder ,這可以否定使用 StringBuilder 的效能優勢。 如需詳細資訊,請參閱 在 StringBuilder 物件中搜尋文字 一節。

請考慮在下列情況下使用 類別 StringBuilder

  • 當您預期程式碼在設計 (時間對字串進行未知數目的變更時,例如,當您使用迴圈串連包含使用者輸入) 的隨機字串數目時。

  • 當您預期程式碼對字串進行大量變更時。

StringBuilder 的運作方式

屬性 StringBuilder.Length 表示物件目前包含的字元 StringBuilder 數。 如果您將字元新增至 StringBuilder 物件,其長度會增加,直到等於 屬性的大小 StringBuilder.Capacity 為止,這會定義物件可以包含的字元數。 如果新增的字元數目導致物件的長度 StringBuilder 超過其目前容量,則會配置新的記憶體、將屬性的值 Capacity 加倍、將新字元新增至 StringBuilder 物件,並調整其 Length 屬性。 物件的額外記憶體 StringBuilder 會動態配置,直到到達 屬性所 StringBuilder.MaxCapacity 定義的值為止。 達到最大容量時,無法為 StringBuilder 物件配置進一步的記憶體,並嘗試新增字元或將其擴充到最大容量會 ArgumentOutOfRangeException 擲回 或 OutOfMemoryException 例外狀況。

下列範例說明物件如何 StringBuilder 配置新的記憶體,並在指派給物件的字串展開時動態增加其容量。 程式碼會 StringBuilder 呼叫其預設 (無參數) 建構函式來建立 物件。 此物件的預設容量為 16 個字元,最大容量超過 20 億個字元。 附加字串「這是句子」,會導致新的記憶體配置,因為字串長度 (19 個字元) 超過物件的預設容量 StringBuilder 。 物件的容量會加倍為 32 個字元、新增字串,而物件的長度現在等於 19 個字元。 然後,程式碼會將字串「這是額外的句子」附加至物件 11 次的值 StringBuilder 。 每當附加作業造成物件的長度 StringBuilder 超過其容量時,其現有的容量會加倍,而且 Append 作業會成功。

using System;
using System.Reflection;
using System.Text;

public class Example
{
   public static void Main()
   {
      StringBuilder sb = new StringBuilder();
      ShowSBInfo(sb);
      sb.Append("This is a sentence.");
      ShowSBInfo(sb);
      for (int ctr = 0; ctr <= 10; ctr++) {
         sb.Append("This is an additional sentence.");
         ShowSBInfo(sb);
      }   
   }
   
   private static void ShowSBInfo(StringBuilder sb)
   {
      foreach (var prop in sb.GetType().GetProperties()) {
         if (prop.GetIndexParameters().Length == 0)
            Console.Write("{0}: {1:N0}    ", prop.Name, prop.GetValue(sb));
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//    Capacity: 16    MaxCapacity: 2,147,483,647    Length: 0
//    Capacity: 32    MaxCapacity: 2,147,483,647    Length: 19
//    Capacity: 64    MaxCapacity: 2,147,483,647    Length: 50
//    Capacity: 128    MaxCapacity: 2,147,483,647    Length: 81
//    Capacity: 128    MaxCapacity: 2,147,483,647    Length: 112
//    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 143
//    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 174
//    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 205
//    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 236
//    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 267
//    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 298
//    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 329
//    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 360
open System.Text

let showSBInfo (sb: StringBuilder) =
    for prop in sb.GetType().GetProperties() do
        if prop.GetIndexParameters().Length = 0 then
            printf $"{prop.Name}: {prop.GetValue sb:N0}    "

    printfn ""

let sb = StringBuilder()
showSBInfo sb
sb.Append "This is a sentence." |> ignore
showSBInfo sb

for i = 0 to 10 do
    sb.Append "This is an additional sentence." |> ignore
    showSBInfo sb

// The example displays the following output:
//    Capacity: 16    MaxCapacity: 2,147,483,647    Length: 0
//    Capacity: 32    MaxCapacity: 2,147,483,647    Length: 19
//    Capacity: 64    MaxCapacity: 2,147,483,647    Length: 50
//    Capacity: 128    MaxCapacity: 2,147,483,647    Length: 81
//    Capacity: 128    MaxCapacity: 2,147,483,647    Length: 112
//    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 143
//    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 174
//    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 205
//    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 236
//    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 267
//    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 298
//    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 329
//    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 360
Imports System.Reflection
Imports System.Text

Module Example
   Public Sub Main()
      Dim sb As New StringBuilder()
      ShowSBInfo(sb)
      sb.Append("This is a sentence.")
      ShowSbInfo(sb)
      For ctr As Integer = 0 To 10
         sb.Append("This is an additional sentence.")
         ShowSbInfo(sb)
      Next   
   End Sub
   
   Public Sub ShowSBInfo(sb As StringBuilder)
      For Each prop In sb.GetType().GetProperties
         If prop.GetIndexParameters().Length = 0 Then
            Console.Write("{0}: {1:N0}    ", prop.Name, prop.GetValue(sb))
         End If   
      Next
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'    Capacity: 16    MaxCapacity: 2,147,483,647    Length: 0
'    Capacity: 32    MaxCapacity: 2,147,483,647    Length: 19
'    Capacity: 64    MaxCapacity: 2,147,483,647    Length: 50
'    Capacity: 128    MaxCapacity: 2,147,483,647    Length: 81
'    Capacity: 128    MaxCapacity: 2,147,483,647    Length: 112
'    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 143
'    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 174
'    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 205
'    Capacity: 256    MaxCapacity: 2,147,483,647    Length: 236
'    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 267
'    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 298
'    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 329
'    Capacity: 512    MaxCapacity: 2,147,483,647    Length: 360

記憶體配置

物件的預設容量 StringBuilder 為 16 個字元,其預設容量上限為 Int32.MaxValue 。 如果您呼叫 和 StringBuilder(String) 建構函式, StringBuilder() 則會使用這些預設值。

您可以透過下列方式明確定義物件的初始容量 StringBuilder

  • 當您建立 物件時,呼叫包含 capacity 參數的任何 StringBuilder 建構函式。

  • 藉由明確將新值指派給 StringBuilder.Capacity 屬性,以展開現有的 StringBuilder 物件。 請注意,如果新的容量小於現有容量或大於 StringBuilder 物件的最大容量,則 屬性會擲回例外狀況。

  • 使用新的容量呼叫 StringBuilder.EnsureCapacity 方法。 新的容量不能大於 StringBuilder 物件的最大容量。 不過,不同于屬性的指派 Capacity ,如果所需的新容量小於現有的容量, EnsureCapacity 就不會擲回例外狀況;在此情況下,方法呼叫不會有任何作用。

如果指派給 StringBuilder 建構函式呼叫中物件之字串的長度超過預設容量或指定的容量, Capacity 則 屬性會設定為使用 value 參數指定的字串長度。

您可以藉由呼叫 StringBuilder(Int32, Int32) 建構函式,明確定義 物件的最大 StringBuilder 容量。 您無法藉由將新值指派給 MaxCapacity 屬性來變更最大容量,因為它是唯讀的。

如上一節所示,每當現有的容量不足時,就會配置額外的記憶體,而且物件的容量 StringBuilder 會加倍至 屬性所 MaxCapacity 定義的值。

一般而言,預設容量和最大容量適用于大部分的應用程式。 您可能會考慮在下列情況下設定這些值:

  • 如果物件的最終大小 StringBuilder 可能成長過大,通常超過數 MB。 在此情況下,將初始 Capacity 屬性設定為相當高的值,以消除太多記憶體重新配置的需求,可能會有一些效能優勢。

  • 如果您的程式碼是在記憶體有限的系統上執行。 在此情況下,您可能想要考慮將 屬性設定 MaxCapacity 為小於 Int32.MaxValue ,如果您的程式碼正在處理可能導致它在記憶體限制的環境中執行的大型字串。

具現化 StringBuilder 物件

您可以藉由呼叫其六個多載類別建構函式的其中一個 StringBuilder 來具現化 物件,如下表所列。 其中三個建構函式會具現化 StringBuilder 其值為空字串的物件,但會以不同的方式設定其 CapacityMaxCapacity 值。 其餘三個 StringBuilder 建構函式會定義具有特定字串值和容量的物件。 三個建構函式中的兩個會使用 的預設最大容量 Int32.MaxValue ,而第三個建構函式可讓您設定最大容量。

建構函式 字串值 Capacity 最大容量
StringBuilder() String.Empty 16 Int32.MaxValue
StringBuilder(Int32) String.Empty capacity 參數定義 Int32.MaxValue
StringBuilder(Int32, Int32) String.Empty capacity 參數定義 maxCapacity 參數定義
StringBuilder(String) value 參數定義 16 或 valueLength,無論哪一個都更大 Int32.MaxValue
StringBuilder(String, Int32) value 參數定義 capacity 參數或 value 所定義。 Length,無論哪一個都更大。 Int32.MaxValue
StringBuilder(String, Int32, Int32, Int32) value 定義。 Substring(startIndex, length) capacity 參數或 value 所定義。 Length,無論哪一個都更大。 Int32.MaxValue

下列範例會使用其中三個建構函式多載來具現化 StringBuilder 物件。

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      string value = "An ordinary string";
      int index = value.IndexOf("An ") + 3;
      int capacity = 0xFFFF;
      
      // Instantiate a StringBuilder from a string.
      StringBuilder sb1 = new StringBuilder(value);
      ShowSBInfo(sb1); 
      
      // Instantiate a StringBuilder from string and define a capacity.  
      StringBuilder sb2 = new StringBuilder(value, capacity);   
      ShowSBInfo(sb2); 
      
      // Instantiate a StringBuilder from substring and define a capacity.  
      StringBuilder sb3 = new StringBuilder(value, index, 
                                            value.Length - index, 
                                            capacity );
      ShowSBInfo(sb3); 
   }

   public static void ShowSBInfo(StringBuilder sb)
   {
      Console.WriteLine("\nValue: {0}", sb.ToString());
      foreach (var prop in sb.GetType().GetProperties()) {
         if (prop.GetIndexParameters().Length == 0)
            Console.Write("{0}: {1:N0}    ", prop.Name, prop.GetValue(sb));
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//    Value: An ordinary string
//    Capacity: 18    MaxCapacity: 2,147,483,647    Length: 18
//    
//    Value: An ordinary string
//    Capacity: 65,535    MaxCapacity: 2,147,483,647    Length: 18
//    
//    Value: ordinary string
//    Capacity: 65,535    MaxCapacity: 2,147,483,647    Length: 15
open System.Text

let showSBInfo (sb: StringBuilder) =
    for prop in sb.GetType().GetProperties() do
        if prop.GetIndexParameters().Length = 0 then
            printf $"{prop.Name}: {prop.GetValue sb:N0}    "

    printfn ""

let value = "An ordinary string"
let index = value.IndexOf "An " + 3
let capacity = 0xFFFF

// Instantiate a StringBuilder from a string.
let sb1 = StringBuilder value
showSBInfo sb1

// Instantiate a StringBuilder from string and define a capacity.
let sb2 = StringBuilder(value, capacity)
showSBInfo sb2

// Instantiate a StringBuilder from substring and define a capacity.
let sb3 = StringBuilder(value, index, value.Length - index, capacity)
showSBInfo sb3

// The example displays the following output:
//    Value: An ordinary string
//    Capacity: 18    MaxCapacity: 2,147,483,647    Length: 18
//
//    Value: An ordinary string
//    Capacity: 65,535    MaxCapacity: 2,147,483,647    Length: 18
//
//    Value: ordinary string
//    Capacity: 65,535    MaxCapacity: 2,147,483,647    Length: 15
Imports System.Text

Module Example
   Public Sub Main()
      Dim value As String = "An ordinary string"
      Dim index As Integer = value.IndexOf("An ") + 3
      Dim capacity As Integer = &hFFFF
      
      ' Instantiate a StringBuilder from a string.
      Dim sb1 As New StringBuilder(value)
      ShowSBInfo(sb1) 
      
      ' Instantiate a StringBuilder from string and define a capacity.  
      Dim sb2 As New StringBuilder(value, capacity)   
      ShowSBInfo(sb2) 
      
      ' Instantiate a StringBuilder from substring and define a capacity.  
      Dim sb3 As New StringBuilder(value, index, 
                                   value.Length - index, 
                                   capacity )
      ShowSBInfo(sb3) 
   End Sub
   
   Public Sub ShowSBInfo(sb As StringBuilder)
      Console.WriteLine()
      Console.WriteLine("Value: {0}", sb.ToString())
      For Each prop In sb.GetType().GetProperties
         If prop.GetIndexParameters().Length = 0 Then
            Console.Write("{0}: {1:N0}    ", prop.Name, prop.GetValue(sb))
         End If   
      Next
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'    Value: An ordinary string
'    Capacity: 18    MaxCapacity: 2,147,483,647    Length: 18
'    
'    Value: An ordinary string
'    Capacity: 65,535    MaxCapacity: 2,147,483,647    Length: 18
'    
'    Value: ordinary string
'    Capacity: 65,535    MaxCapacity: 2,147,483,647    Length: 15

呼叫 StringBuilder 方法

大部分修改 實例中 StringBuilder 字串的方法都會傳回該相同實例的參考。 這可讓您以兩種方式呼叫 StringBuilder 方法:

  • 您可以進行個別方法呼叫,並忽略傳回值,如下列範例所示。

    using System;
    using System.Text;
    
    public class Example
    {
       public static void Main()
       {
          StringBuilder sb = new StringBuilder();
          sb.Append("This is the beginning of a sentence, ");
          sb.Replace("the beginning of ", "");
          sb.Insert(sb.ToString().IndexOf("a ") + 2, "complete ");
          sb.Replace(",", ".");
          Console.WriteLine(sb.ToString());
       }
    }
    // The example displays the following output:
    //        This is a complete sentence.
    
    open System.Text
    
    let sb = StringBuilder()
    sb.Append "This is the beginning of a sentence, " |> ignore
    sb.Replace("the beginning of ", "") |> ignore
    sb.Insert((string sb).IndexOf "a " + 2, "complete ") |> ignore
    sb.Replace(",", ".") |> ignore
    printfn $"{sb}"
    // The example displays the following output:
    //        This is a complete sentence.
    
    Imports System.Text
    
    Module Example
       Public Sub Main()
          Dim sb As New StringBuilder()
          sb.Append("This is the beginning of a sentence, ")
          sb.Replace("the beginning of ", "")
          sb.Insert(sb.ToString().IndexOf("a ") + 2, "complete ")
          sb.Replace(",", ".")
          Console.WriteLine(sb.ToString())
       End Sub
    End Module
    ' The example displays the following output:
    '       This is a complete sentence.
    
  • 您可以在單一語句中進行一系列的方法呼叫。 如果您想要撰寫鏈結後續作業的單一語句,這會很方便。 下列範例會將上一個範例中的三個方法呼叫合併成單行程式碼。

    using System;
    using System.Text;
    
    public class Example
    {
       public static void Main()
       {
          StringBuilder sb = new StringBuilder("This is the beginning of a sentence, ");
          sb.Replace("the beginning of ", "").Insert(sb.ToString().IndexOf("a ") + 2, 
                                                     "complete ").Replace(",", ".");
          Console.WriteLine(sb.ToString());
       }
    }
    // The example displays the following output:
    //        This is a complete sentence.
    
    open System.Text
    
    let sb = StringBuilder "This is the beginning of a sentence, "
    
    sb
        .Replace("the beginning of ", "")
        .Insert((string sb).IndexOf "a " + 2, "complete ")
        .Replace(",", ".")
    |> ignore
    
    printfn $"{sb}"
    // The example displays the following output:
    //        This is a complete sentence.
    
    Imports System.Text
    
    Module Example
       Public Sub Main()
          Dim sb As New StringBuilder("This is the beginning of a sentence, ")
          sb.Replace("the beginning of ", "").Insert(sb.ToString().IndexOf("a ") + 2, _
                                                     "complete ").Replace(", ", ".")
          Console.WriteLine(sb.ToString())
       End Sub
    End Module
    ' The example displays the following output:
    '       This is a complete sentence.
    

執行 StringBuilder 作業

您可以使用 類別的 StringBuilder 方法來逐一查看、新增、刪除或修改 物件中的 StringBuilder 字元。

反覆運算 StringBuilder 字元

您可以使用 屬性來存取 物件 StringBuilder.Chars[] 中的 StringBuilder 字元。 在 C# 中, Chars[] 是索引子;在 Visual Basic 中,它是 類別的預設屬性 StringBuilder 。 這可讓您只使用個別字元的索引來設定或擷取個別字元,而不需要明確參考 Chars[] 屬性。 物件中的 StringBuilder 字元從索引 0 開始, (零) 並繼續編制索引 Length - 1。

下列範例說明 Chars[] 屬性。 它會將十個亂數字附加至 物件,然後逐一 StringBuilder 查看每個字元。 如果字元的 Unicode 類別是 UnicodeCategory.DecimalDigitNumber ,它會將數位減少 1 (,如果數位值為 0) ,則數位會變更為 9。 此範例會顯示變更個別字元值前後的物件內容 StringBuilder

using System;
using System.Globalization;
using System.Text;

public class Example
{
   public static void Main()
   {
      Random rnd = new Random();
      StringBuilder sb = new StringBuilder();
      
      // Generate 10 random numbers and store them in a StringBuilder.
      for (int ctr = 0; ctr <= 9; ctr++)
         sb.Append(rnd.Next().ToString("N5"));    

      Console.WriteLine("The original string:");
      Console.WriteLine(sb.ToString());
            
      // Decrease each number by one.
      for (int ctr = 0; ctr < sb.Length; ctr++) {
         if (Char.GetUnicodeCategory(sb[ctr]) == UnicodeCategory.DecimalDigitNumber) {
            int number = (int) Char.GetNumericValue(sb[ctr]);
            number--;
            if (number < 0) number = 9;
         
            sb[ctr] = number.ToString()[0];
         }
      }
      Console.WriteLine("\nThe new string:");
      Console.WriteLine(sb.ToString());
   }
}
// The example displays the following output:
//    The original string:
//    1,457,531,530.00000940,522,609.000001,668,113,564.000001,998,992,883.000001,792,660,834.00
//    000101,203,251.000002,051,183,075.000002,066,000,067.000001,643,701,043.000001,702,382,508
//    .00000
//    
//    The new string:
//    0,346,420,429.99999839,411,598.999990,557,002,453.999990,887,881,772.999990,681,559,723.99
//    999090,192,140.999991,940,072,964.999991,955,999,956.999990,532,690,932.999990,691,271,497
//    .99999
open System
open System.Globalization
open System.Text

let rnd = Random()
let sb = new StringBuilder()

// Generate 10 random numbers and store them in a StringBuilder.
for _ = 0 to 9 do
    rnd.Next().ToString "N5" |> sb.Append |> ignore

printfn "The original string:"
printfn $"{sb}"

// Decrease each number by one.
for i = 0 to sb.Length - 1 do
    if Char.GetUnicodeCategory(sb[i]) = UnicodeCategory.DecimalDigitNumber then
        let number = Char.GetNumericValue sb.[i] |> int
        let number = number - 1
        let number = if number < 0 then 9 else number
        sb.[i] <- number.ToString()[0]

printfn "\nThe new string:"
printfn $"{sb}"

// The example displays the following output:
//    The original string:
//    1,457,531,530.00000940,522,609.000001,668,113,564.000001,998,992,883.000001,792,660,834.00
//    000101,203,251.000002,051,183,075.000002,066,000,067.000001,643,701,043.000001,702,382,508
//    .00000
//
//    The new string:
//    0,346,420,429.99999839,411,598.999990,557,002,453.999990,887,881,772.999990,681,559,723.99
//    999090,192,140.999991,940,072,964.999991,955,999,956.999990,532,690,932.999990,691,271,497
//    .99999
Imports System.Globalization
Imports System.Text

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      Dim sb As New StringBuilder()
      
      ' Generate 10 random numbers and store them in a StringBuilder.
      For ctr As Integer = 0 To 9
         sb.Append(rnd.Next().ToString("N5"))    
      Next
      Console.WriteLine("The original string:")
      Console.WriteLine(sb.ToString())
      Console.WriteLine()
            
      ' Decrease each number by one.
      For ctr As Integer = 0 To sb.Length - 1
         If Char.GetUnicodeCategory(sb(ctr)) = UnicodeCategory.DecimalDigitNumber Then
            Dim number As Integer = CType(Char.GetNumericValue(sb(ctr)), Integer)
            number -= 1
            If number < 0 Then number = 9
         
            sb(ctr) = number.ToString()(0)
         End If
      Next
      Console.WriteLine("The new string:")
      Console.WriteLine(sb.ToString())
   End Sub
End Module
' The example displays the following output:
'    The original string:
'    1,457,531,530.00000940,522,609.000001,668,113,564.000001,998,992,883.000001,792,660,834.00
'    000101,203,251.000002,051,183,075.000002,066,000,067.000001,643,701,043.000001,702,382,508
'    .00000
'    
'    The new string:
'    0,346,420,429.99999839,411,598.999990,557,002,453.999990,887,881,772.999990,681,559,723.99
'    999090,192,140.999991,940,072,964.999991,955,999,956.999990,532,690,932.999990,691,271,497
'    .99999

在下列狀況下,搭配使用以字元為主的索引與 Chars[] 屬性可能極為緩慢:

由於每個字元的存取會行經整個連結的區塊清單來尋找要編入索引的正確緩衝區,因此會嚴重影響效能。

注意

即使是大型「區塊」 StringBuilder 物件,使用 Chars[] 屬性來存取一或少數位符時,還是會有可忽略的效能影響;一般而言,它是 O (n 個) 作業。 逐一查看 StringBuilder 物件中的字元時,就會發生顯著的效能影響,這是 O(n^2) 作業。

如果在 StringBuilder 物件中使用以字元為主的索引時遇到效能問題,您可以使用下列任一因應措施:

  • 藉由呼叫 ToString 方法將 StringBuilder 執行個體轉換為 String,然後存取字串中的字元。

  • 將現有 StringBuilder 物件的內容複製到預留大小的新 StringBuilder 物件。 因為新的 StringBuilder 物件不是塊狀,所以會改善效能。 例如:

    // sbOriginal is the existing StringBuilder object
    var sbNew = new StringBuilder(sbOriginal.ToString(), sbOriginal.Length);
    
    ' sbOriginal is the existing StringBuilder object
    Dim sbNew = New StringBuilder(sbOriginal.ToString(), sbOriginal.Length)
    
  • 藉由呼叫 StringBuilder(Int32) 建構函式,將 StringBuilder 物件的初始容量設定為大約等於其預期大小上限的值。 請注意,即使 StringBuilder 很少達到其最大容量,這也會配置整個記憶體區塊。

將文字新增至 StringBuilder 物件

類別 StringBuilder 包含下列方法來擴充 物件的內容 StringBuilder

  • 方法 Append 會將字串、子字串、字元陣列、字元陣列的一部分、重複多次的單一字元,或基本資料類型的字串表示附加至 StringBuilder 物件。

  • 方法 AppendLine 會將行結束字元或字串以及行結束字元附加至 StringBuilder 物件。

  • 方法 AppendFormat 會將 複合格式字串 附加至 StringBuilder 物件。 結果字串中包含的物件字串表示可以反映目前系統文化特性或指定文化特性的格式慣例。

  • 方法 Insert 會插入字串、子字串、字串的多個重複、字元陣列、字元陣列的一部分,或物件中指定位置之基本資料類型的 StringBuilder 字串表示。 位置是由以零起始的索引所定義。

下列範例會 Append 使用 、 AppendLineAppendFormatInsert 方法來展開 物件的文字 StringBuilder

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      // Create a StringBuilder object with no text.
      StringBuilder sb = new StringBuilder();
      // Append some text.
      sb.Append('*', 10).Append(" Adding Text to a StringBuilder Object ").Append('*', 10);
      sb.AppendLine("\n");
      sb.AppendLine("Some code points and their corresponding characters:");
      // Append some formatted text.
      for (int ctr = 50; ctr <= 60; ctr++) {
         sb.AppendFormat("{0,12:X4} {1,12}", ctr, Convert.ToChar(ctr));
         sb.AppendLine();
      }
      // Find the end of the introduction to the column.
      int pos = sb.ToString().IndexOf("characters:") + 11 + 
                Environment.NewLine.Length;
      // Insert a column header.
      sb.Insert(pos, String.Format("{2}{0,12:X4} {1,12}{2}", "Code Unit", 
                                   "Character", "\n"));      

      // Convert the StringBuilder to a string and display it.      
      Console.WriteLine(sb.ToString());      
   }
}
// The example displays the following output:
//    ********** Adding Text to a StringBuilder Object **********
//    
//    Some code points and their corresponding characters:
//    
//       Code Unit    Character
//            0032            2
//            0033            3
//            0034            4
//            0035            5
//            0036            6
//            0037            7
//            0038            8
//            0039            9
//            003A            :
//            003B            ;
//            003C            <
open System
open System.Text

// Create a StringBuilder object with no text.
let sb = StringBuilder()
// Append some text.
sb
    .Append('*', 10)
    .Append(" Adding Text to a StringBuilder Object ")
    .Append('*', 10)
|> ignore

sb.AppendLine "\n" |> ignore
sb.AppendLine "Some code points and their corresponding characters:" |> ignore
// Append some formatted text.
for i = 50 to 60 do
    sb.AppendFormat("{0,12:X4} {1,12}", i, Convert.ToChar i) |> ignore
    sb.AppendLine() |> ignore

// Find the end of the introduction to the column.
let pos = (string sb).IndexOf("characters:") + 11 + Environment.NewLine.Length
// Insert a column header.
sb.Insert(pos, String.Format("{2}{0,12:X4} {1,12}{2}", "Code Unit", "Character", "\n"))
|> ignore

// Convert the StringBuilder to a string and display it.
printfn $"{sb}"


// The example displays the following output:
//    ********** Adding Text to a StringBuilder Object **********
//
//    Some code points and their corresponding characters:
//
//       Code Unit    Character
//            0032            2
//            0033            3
//            0034            4
//            0035            5
//            0036            6
//            0037            7
//            0038            8
//            0039            9
//            003A            :
//            003B            ;
//            003C            <
Imports System.Text

Module Example
   Public Sub Main()
      ' Create a StringBuilder object with no text.
      Dim sb As New StringBuilder()
      ' Append some text.
      sb.Append("*"c, 10).Append(" Adding Text to a StringBuilder Object ").Append("*"c, 10)
      sb.AppendLine()
      sb.AppendLine()
      sb.AppendLine("Some code points and their corresponding characters:")
      ' Append some formatted text.
      For ctr = 50 To 60
         sb.AppendFormat("{0,12:X4} {1,12}", ctr, Convert.ToChar(ctr))
         sb.AppendLine()
      Next
      ' Find the end of the introduction to the column.
      Dim pos As Integer = sb.ToString().IndexOf("characters:") + 11 + 
                           Environment.NewLine.Length
      ' Insert a column header.
      sb.Insert(pos, String.Format("{2}{0,12:X4} {1,12}{2}", "Code Unit", 
                                   "Character", vbCrLf))      

      ' Convert the StringBuilder to a string and display it.      
      Console.WriteLine(sb.ToString())      
   End Sub
End Module
' The example displays the following output:
'       ********** Adding Text to a StringBuilder Object **********
'       
'       Some code points and their corresponding characters:
'       
'          Code Unit    Character
'               0032            2
'               0033            3
'               0034            4
'               0035            5
'               0036            6
'               0037            7
'               0038            8
'               0039            9
'               003A            :
'               003B            ;
'               003C            <

從 StringBuilder 物件刪除文字

類別 StringBuilder 包含可減少目前 StringBuilder 實例大小的方法。 方法 Clear 會移除所有字元,並將 屬性設定 Length 為零。 方法 Remove 會刪除從特定索引位置開始的指定字元數。 此外,您可以將物件的 屬性設定 Length 為小於目前實例長度的值,以從物件的結尾 StringBuilder 移除字元。

下列範例會從 StringBuilder 物件中移除部分文字、顯示其產生的容量、最大容量和長度屬性值,然後呼叫 Clear 方法,以移除 物件中的所有字元 StringBuilder

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      StringBuilder sb = new StringBuilder("A StringBuilder object");
      ShowSBInfo(sb);
      // Remove "object" from the text.
      string textToRemove = "object";
      int pos = sb.ToString().IndexOf(textToRemove);
      if (pos >= 0) {
         sb.Remove(pos, textToRemove.Length);
         ShowSBInfo(sb);
      }
      // Clear the StringBuilder contents.
      sb.Clear();
      ShowSBInfo(sb);   
   }

   public static void ShowSBInfo(StringBuilder sb)
   {
      Console.WriteLine("\nValue: {0}", sb.ToString());
      foreach (var prop in sb.GetType().GetProperties()) {
         if (prop.GetIndexParameters().Length == 0)
            Console.Write("{0}: {1:N0}    ", prop.Name, prop.GetValue(sb));
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//    Value: A StringBuilder object
//    Capacity: 22    MaxCapacity: 2,147,483,647    Length: 22
//    
//    Value: A StringBuilder
//    Capacity: 22    MaxCapacity: 2,147,483,647    Length: 16
//    
//    Value:
//    Capacity: 22    MaxCapacity: 2,147,483,647    Length: 0
open System.Text

let showSBInfo (sb: StringBuilder) =
    for prop in sb.GetType().GetProperties() do
        if prop.GetIndexParameters().Length = 0 then
            printf $"{prop.Name}: {prop.GetValue sb:N0}    "

    printfn ""

let sb = StringBuilder "A StringBuilder object"
showSBInfo sb
// Remove "object" from the text.
let textToRemove = "object"
let pos = (string sb).IndexOf textToRemove

if pos >= 0 then
    sb.Remove(pos, textToRemove.Length) |> ignore
    showSBInfo sb

// Clear the StringBuilder contents.
sb.Clear() |> ignore
showSBInfo sb

// The example displays the following output:
//    Value: A StringBuilder object
//    Capacity: 22    MaxCapacity: 2,147,483,647    Length: 22
//
//    Value: A StringBuilder
//    Capacity: 22    MaxCapacity: 2,147,483,647    Length: 16
//
//    Value:
//    Capacity: 22    MaxCapacity: 2,147,483,647    Length: 0
Imports System.Text

Module Example
   Public Sub Main()
      Dim sb As New StringBuilder("A StringBuilder object")
      ShowSBInfo(sb)
      ' Remove "object" from the text.
      Dim textToRemove As String = "object"
      Dim pos As Integer = sb.ToString().IndexOf(textToRemove)
      If pos >= 0
         sb.Remove(pos, textToRemove.Length)
         ShowSBInfo(sb)
      End If
      ' Clear the StringBuilder contents.
      sb.Clear()
      ShowSBInfo(sb)   
   End Sub

   Public Sub ShowSBInfo(sb As StringBuilder)
      Console.WriteLine()
      Console.WriteLine("Value: {0}", sb.ToString())
      For Each prop In sb.GetType().GetProperties
         If prop.GetIndexParameters().Length = 0 Then
            Console.Write("{0}: {1:N0}    ", prop.Name, prop.GetValue(sb))
         End If   
      Next
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'    Value: A StringBuilder object
'    Capacity: 22    MaxCapacity: 2,147,483,647    Length: 22
'    
'    Value: A StringBuilder
'    Capacity: 22    MaxCapacity: 2,147,483,647    Length: 16
'    
'    Value:
'    Capacity: 22    MaxCapacity: 2,147,483,647    Length: 0

修改 StringBuilder 物件中的文字

方法 StringBuilder.Replace 會取代整個 StringBuilder 物件或特定字元範圍中所有出現的字元或字串。 下列範例會 Replace 使用 方法,將 物件中的所有 StringBuilder 驚嘆號 (!) 取代為問號 (?) 。

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
      MyStringBuilder.Replace('!', '?');
      Console.WriteLine(MyStringBuilder);
   }
}
// The example displays the following output:
//       Hello World?
open System.Text

let myStringBuilder = StringBuilder "Hello World!"
myStringBuilder.Replace('!', '?') |> ignore
printfn $"{myStringBuilder}"

// The example displays the following output:
//       Hello World?
Imports System.Text

Module Example
   Public Sub Main()
      Dim MyStringBuilder As New StringBuilder("Hello World!")
      MyStringBuilder.Replace("!"c, "?"c)
      Console.WriteLine(MyStringBuilder)
   End Sub
End Module
' The example displays the following output:
'       Hello World?

搜尋 StringBuilder 物件中的文字

類別 StringBuilder 不包含類似 String.Contains 類別所提供的 StringString.IndexOfString.StartsWith 方法的方法,可讓您搜尋物件中的特定字元或子字串。 若要判斷子字串是否存在或起始字元位置,您必須使用字串搜尋方法或正則運算式方法來搜尋 String 值。 有四種方式可以實作這類搜尋,如下表所示。

技巧 優點 缺點
先搜尋字串值,再將它們新增至 StringBuilder 物件。 適用于判斷子字串是否存在。 當子字串的索引位置很重要時,便無法使用。
呼叫 ToString 並搜尋傳 String 回的物件。 如果您將所有文字指派給 StringBuilder 物件,然後開始修改它,則很容易使用。 如果您必須先進行修改,才能將所有文字新增至 StringBuilder 物件,則重複呼叫 ToString 會很麻煩。

如果您要進行變更, StringBuilder 您必須記得從物件的文字結尾工作。
Chars[]使用 屬性來循序搜尋字元範圍。 如果您擔心個別字元或小型子字串,則很有用。 如果要搜尋的字元數很大,或搜尋邏輯很複雜,則很麻煩。

對於透過重複的方法呼叫而成長非常大的物件,會產生非常差的效能。
StringBuilder 物件轉換成 String 物件,並在 物件上 String 執行修改。 如果修改數目很小,則很有用。 如果修改數目很大, StringBuilder 則否定 類別的效能優勢。

讓我們更詳細地檢查這些技術。

  • 如果搜尋的目標是判斷特定子字串是否存在 (也就是說,如果您對子字串) 的位置不感興趣,則可以先搜尋字串,再將它們儲存在 物件中 StringBuilder 。 下列範例提供一個可能的實作。 它會定義類別 StringBuilderFinder ,其建構函式會傳遞物件參考, StringBuilder 以及要于字串中尋找的子字串。 在此情況下,此範例會嘗試判斷記錄的溫度是否為華氏或攝氏,並將適當的簡介文字新增至物件的開頭 StringBuilder 。 亂數產生器可用來選取陣列,其中包含攝氏或華氏度的資料。

    using System;
    using System.Text;
    
    public class Example
    {
       public static void Main()
       {
          Random rnd = new Random();
          string[] tempF = { "47.6F", "51.3F", "49.5F", "62.3F" };
          string[] tempC = { "21.2C", "16.1C", "23.5C", "22.9C" };
          string[][] temps = { tempF, tempC }; 
    
          StringBuilder sb = new StringBuilder();
          var f = new StringBuilderFinder(sb, "F");
          var baseDate = new DateTime(2013, 5, 1); 
          String[] temperatures = temps[rnd.Next(2)];
          bool isFahrenheit = false;
          foreach (var temperature in temperatures) {
             if (isFahrenheit)
                sb.AppendFormat("{0:d}: {1}\n", baseDate, temperature);
             else
                isFahrenheit = f.SearchAndAppend(String.Format("{0:d}: {1}\n", 
                                                 baseDate, temperature));
             baseDate = baseDate.AddDays(1);
          }            
          if (isFahrenheit) {
             sb.Insert(0, "Average Daily Temperature in Degrees Fahrenheit");
             sb.Insert(47, "\n\n");
          }
          else {
             sb.Insert(0, "Average Daily Temperature in Degrees Celsius");
             sb.Insert(44, "\n\n");
          }   
          Console.WriteLine(sb.ToString());
       }
    }
    
    public class StringBuilderFinder
    {
       private StringBuilder sb;
       private String text;
       
       public StringBuilderFinder(StringBuilder sb, String textToFind)
       {
          this.sb = sb;
          this.text = textToFind;
       }
       
       public bool SearchAndAppend(String stringToSearch)
       {
          sb.Append(stringToSearch);
          return stringToSearch.Contains(text);
       }
    }
    // The example displays output similar to the following:
    //    Average Daily Temperature in Degrees Celsius
    //    
    //    5/1/2013: 21.2C
    //    5/2/2013: 16.1C
    //    5/3/2013: 23.5C
    //    5/4/2013: 22.9C
    
    open System
    open System.Text
    
    type StringBuilderFinder(sb: StringBuilder, textToFind: string) =
        member _.SearchAndAppend(stringToSearch: string) =
            sb.Append stringToSearch |> ignore
            stringToSearch.Contains textToFind
    
    let tempF = [| "47.6F"; "51.3F"; "49.5F"; "62.3F" |]
    let tempC = [| "21.2C"; "16.1C"; "23.5C"; "22.9C" |]
    let temps = [| tempF; tempC |]
    
    let sb = StringBuilder()
    let f = StringBuilderFinder(sb, "F")
    let temperatures = temps[Random.Shared.Next(2)]
    let mutable baseDate = DateTime(2013, 5, 1)
    let mutable isFahrenheit = false
    
    for temperature in temperatures do
        if isFahrenheit then
            sb.AppendFormat("{0:d}: {1}\n", baseDate, temperature) |> ignore
        else
            isFahrenheit <- $"{baseDate:d}: {temperature}\n" |> f.SearchAndAppend
    
        baseDate <- baseDate.AddDays 1
    
    if isFahrenheit then
        sb.Insert(0, "Average Daily Temperature in Degrees Fahrenheit") |> ignore
        sb.Insert(47, "\n\n") |> ignore
    
    else
        sb.Insert(0, "Average Daily Temperature in Degrees Celsius") |> ignore
        sb.Insert(44, "\n\n") |> ignore
    
    printfn $"{sb}"
    
    // The example displays output similar to the following:
    //    Average Daily Temperature in Degrees Celsius
    //
    //    5/1/2013: 21.2C
    //    5/2/2013: 16.1C
    //    5/3/2013: 23.5C
    //    5/4/2013: 22.9C
    
    Imports System.Text
    
    Module Example
       Public Sub Main()
          Dim rnd As New Random()
          Dim tempF() As String = { "47.6F", "51.3F", "49.5F", "62.3F" }
          Dim tempC() As String = { "21.2C", "16.1C", "23.5C", "22.9C" }
          Dim temps()() As String = { tempF, tempC } 
    
          Dim sb As StringBuilder = New StringBuilder()
          Dim f As New StringBuilderFinder(sb, "F")
          Dim baseDate As New DateTime(2013, 5, 1) 
          Dim temperatures() As String = temps(rnd.Next(2))
          Dim isFahrenheit As Boolean = False
          For Each temperature In temperatures
             If isFahrenheit Then
                sb.AppendFormat("{0:d}: {1}{2}", baseDate, temperature, vbCrLf)
             Else
                isFahrenheit = f.SearchAndAppend(String.Format("{0:d}: {1}{2}", 
                                                 baseDate, temperature, vbCrLf))
             End If
             baseDate = baseDate.AddDays(1)
          Next            
          If isFahrenheit Then
             sb.Insert(0, "Average Daily Temperature in Degrees Fahrenheit")
             sb.Insert(47, vbCrLf + vbCrLf)
          Else
             sb.Insert(0, "Average Daily Temperature in Degrees Celsius")
             sb.Insert(44, vbCrLf + vbCrLf)
          End If   
          Console.WriteLine(sb.ToString())
       End Sub
    End Module
    
    Public Class StringBuilderFinder
       Private sb As StringBuilder
       Private text As String
       
       Public Sub New(sb As StringBuilder, textToFind As String)
          Me.sb = sb
          text = textToFind
       End Sub
       
       Public Function SearchAndAppend(stringToSearch As String) As Boolean
          sb.Append(stringToSearch)
          Return stringToSearch.Contains(text)
       End Function
    End Class
    ' The example displays output similar to the following:
    '    Average Daily Temperature in Degrees Celsius
    '    
    '    5/1/2013: 21.2C
    '    5/2/2013: 16.1C
    '    5/3/2013: 23.5C
    '    5/4/2013: 22.9C
    
  • StringBuilder.ToString呼叫 方法,將 StringBuilder 物件 String 轉換成 物件。 您可以使用 或 String.StartsWith 之類的 String.LastIndexOf 方法來搜尋字串,也可以使用正則運算式和 Regex 類別來搜尋模式。 由於 和 String 物件都 StringBuilder 使用 UTF-16 編碼來儲存字元,所以兩個物件中的字元、子字串和正則運算式相符專案的索引位置都相同。 這可讓您使用 StringBuilder 方法來在 物件中找到 String 該文字的相同位置進行變更。

    注意

    如果您採用這種方法,您應該從物件的結尾 StringBuilder 到其開頭工作,如此您就不需要重複將物件轉換成 StringBuilder 字串。

    下列範例將示範這個方法。 它會在 物件中儲存四個 StringBuilder 英文字母的出現次數。 然後,它會將文字 String 轉換成 物件,並使用正則運算式來識別每個四個字元序列的起始位置。 最後,它會在每個四個字元序列之前加上底線,但第一個序列除外,並將序列的第一個字元轉換成大寫。

    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          // Create a StringBuilder object with 4 successive occurrences 
          // of each character in the English alphabet. 
          StringBuilder sb = new StringBuilder();
          for (ushort ctr = (ushort)'a'; ctr <= (ushort) 'z'; ctr++)
             sb.Append(Convert.ToChar(ctr), 4);
          
          // Create a parallel string object.
          String sbString = sb.ToString();
          // Determine where each new character sequence begins.
          String pattern = @"(\w)\1+";
          MatchCollection matches = Regex.Matches(sbString, pattern);
    
          // Uppercase the first occurrence of the sequence, and separate it
          // from the previous sequence by an underscore character.
          for (int ctr = matches.Count - 1; ctr >= 0; ctr--) { 
             Match m = matches[ctr];
             sb[m.Index] = Char.ToUpper(sb[m.Index]);
             if (m.Index > 0) sb.Insert(m.Index, "_");
          }
          // Display the resulting string.
          sbString = sb.ToString();
          int line = 0;
          do {
             int nChars = line * 80 + 79 <= sbString.Length ? 
                                 80 : sbString.Length - line * 80;
             Console.WriteLine(sbString.Substring(line * 80, nChars));
             line++;
          } while (line * 80 < sbString.Length);
       }
    }
    // The example displays the following output:
    //    Aaaa_Bbbb_Cccc_Dddd_Eeee_Ffff_Gggg_Hhhh_Iiii_Jjjj_Kkkk_Llll_Mmmm_Nnnn_Oooo_Pppp_
    //    Qqqq_Rrrr_Ssss_Tttt_Uuuu_Vvvv_Wwww_Xxxx_Yyyy_Zzzz
    
    open System
    open System.Text
    open System.Text.RegularExpressions
    
    // Create a StringBuilder object with 4 successive occurrences
    // of each character in the English alphabet.
    let sb = StringBuilder()
    
    for char in 'a' .. 'z' do
        sb.Append(char, 4) |> ignore
    
    // Create a parallel string object.
    let sbString = string sb
    // Determine where each new character sequence begins.
    let pattern = @"(\w)\1+"
    let matches = Regex.Matches(sbString, pattern)
    
    // Uppercase the first occurrence of the sequence, and separate it
    // from the previous sequence by an underscore character.
    for i = matches.Count - 1 downto 0 do
        let m = matches[i]
        sb[m.Index] <- Char.ToUpper sb[m.Index]
    
        if m.Index > 0 then
            sb.Insert(m.Index, "_") |> ignore
    
    // Display the resulting string.
    let sbString2 = string sb
    
    for line = 0 to (sbString2.Length - 1) / 80 do
        let nChars =
            if line * 80 + 79 <= sbString2.Length then
                80
            else
                sbString2.Length - line * 80
    
        printfn $"{sbString2.Substring(line * 80, nChars)}"
    
    
    // The example displays the following output:
    //    Aaaa_Bbbb_Cccc_Dddd_Eeee_Ffff_Gggg_Hhhh_Iiii_Jjjj_Kkkk_Llll_Mmmm_Nnnn_Oooo_Pppp_
    //    Qqqq_Rrrr_Ssss_Tttt_Uuuu_Vvvv_Wwww_Xxxx_Yyyy_Zzzz
    
    Imports System.Text
    Imports System.Text.RegularExpressions
    
    Module Example
       Public Sub Main()
          ' Create a StringBuilder object with 4 successive occurrences 
          ' of each character in the English alphabet. 
          Dim sb As New StringBuilder()
          For ctr As UShort = AscW("a") To Ascw("z")
             sb.Append(ChrW(ctr), 4)
          Next    
          ' Create a parallel string object.
          Dim sbString As String = sb.ToString()
          ' Determine where each new character sequence begins.
          Dim pattern As String = "(\w)\1+"
          Dim matches As MatchCollection = Regex.Matches(sbString, pattern)
    
          ' Uppercase the first occurrence of the sequence, and separate it
          ' from the previous sequence by an underscore character.
          For ctr As Integer = matches.Count - 1 To 0 Step -1 
             Dim m As Match = matches(ctr)
             sb.Chars(m.Index) = Char.ToUpper(sb.Chars(m.index))
             If m.Index > 0 Then sb.Insert(m.Index, "_")
          Next
          ' Display the resulting string.
          sbString = sb.ToString()
          Dim line As Integer = 0
          Do
             Dim nChars As Integer = If(line * 80 + 79 <= sbString.Length, 
                                        80, sbString.Length - line * 80)
             Console.WriteLine(sbString.Substring(line * 80, nChars))
             line += 1
          Loop While line * 80 < sbString.Length
       End Sub
    End Module
    ' The example displays the following output:
    '    Aaaa_Bbbb_Cccc_Dddd_Eeee_Ffff_Gggg_Hhhh_Iiii_Jjjj_Kkkk_Llll_Mmmm_Nnnn_Oooo_Pppp_
    '    Qqqq_Rrrr_Ssss_Tttt_Uuuu_Vvvv_Wwww_Xxxx_Yyyy_Zzzz
    
  • StringBuilder.Chars[]使用 屬性依序搜尋 物件中的 StringBuilder 字元範圍。 如果要搜尋的字元數很大,或搜尋邏輯特別複雜,此方法可能並不實用。 如需非常大型區塊 StringBuilder 物件字元索引型存取的效能影響,請參閱 屬性的檔 StringBuilder.Chars[]

    下列範例在功能上與上一個範例相同,但實作不同。 它會使用 Chars[] 屬性來偵測字元值何時變更、在該位置插入底線,並將新序列中的第一個字元轉換成大寫。

    using System;
    using System.Text;
    
    public class Example
    {
       public static void Main()
       {
          // Create a StringBuilder object with 4 successive occurrences 
          // of each character in the English alphabet. 
          StringBuilder sb = new StringBuilder();
          for (ushort ctr = (ushort) 'a'; ctr <= (ushort) 'z'; ctr++)
             sb.Append(Convert.ToChar(ctr), 4);
    
          // Iterate the text to determine when a new character sequence occurs.
          int position = 0;
          Char current = '\u0000';
          do {
             if (sb[position] != current) {
                current = sb[position];
                sb[position] = Char.ToUpper(sb[position]);
                if (position > 0) 
                   sb.Insert(position, "_");
                position += 2;
             }
             else {
                position++;
             }      
          } while (position <= sb.Length - 1);
          // Display the resulting string.
          String sbString = sb.ToString();
          int line = 0;
          do {
             int nChars = line * 80 + 79 <= sbString.Length ? 
                                 80 : sbString.Length - line * 80;
             Console.WriteLine(sbString.Substring(line * 80, nChars));
             line++;
          } while (line * 80 < sbString.Length);
       }
    }
    // The example displays the following output:
    //    Aaaa_Bbbb_Cccc_Dddd_Eeee_Ffff_Gggg_Hhhh_Iiii_Jjjj_Kkkk_Llll_Mmmm_Nnnn_Oooo_Pppp_
    //    Qqqq_Rrrr_Ssss_Tttt_Uuuu_Vvvv_Wwww_Xxxx_Yyyy_Zzzz
    
    open System
    open System.Text
    
    // Create a StringBuilder object with 4 successive occurrences
    // of each character in the English alphabet.
    let sb = StringBuilder()
    
    for char in 'a' .. 'z' do
        sb.Append(char, 4) |> ignore
    
    // Iterate the text to determine when a new character sequence occurs.
    let mutable position = 0
    let mutable current = '\u0000'
    
    while position <= sb.Length - 1 do
        if sb[position] <> current then
            current <- sb[position]
            sb[position] <- Char.ToUpper sb[position]
    
            if position > 0 then
                sb.Insert(position, "_") |> ignore
    
            position <- position + 2
    
        else
            position <- position + 1
    
    // Display the resulting string.
    let sbString = string sb
    
    for line = 0 to (sbString.Length - 1) / 80 do
        let nChars =
            if line * 80 + 79 <= sbString.Length then
                80
            else
                sbString.Length - line * 80
    
        printfn $"{sbString.Substring(line * 80, nChars)}"
    
    // The example displays the following output:
    //    Aaaa_Bbbb_Cccc_Dddd_Eeee_Ffff_Gggg_Hhhh_Iiii_Jjjj_Kkkk_Llll_Mmmm_Nnnn_Oooo_Pppp_
    //    Qqqq_Rrrr_Ssss_Tttt_Uuuu_Vvvv_Wwww_Xxxx_Yyyy_Zzzz
    
    Imports System.Text
    
    Module Example
       Public Sub Main()
          ' Create a StringBuilder object with 4 successive occurrences 
          ' of each character in the English alphabet. 
          Dim sb As New StringBuilder()
          For ctr As UShort = AscW("a") To Ascw("z")
             sb.Append(ChrW(ctr), 4)
          Next    
          ' Iterate the text to determine when a new character sequence occurs.
          Dim position As Integer = 0
          Dim current As Char = ChrW(0)
          Do
             If sb(position) <> current Then
                current = sb(position)
                sb(position) = Char.ToUpper(sb(position))
                If position > 0 Then sb.Insert(position, "_")
                position += 2
             Else
                position += 1
             End If      
          Loop While position <= sb.Length - 1
          ' Display the resulting string.
          Dim sbString As String = sb.ToString()
          Dim line As Integer = 0
          Do
             Dim nChars As Integer = If(line * 80 + 79 <= sbString.Length, 
                                        80, sbString.Length - line * 80)
             Console.WriteLine(sbString.Substring(line * 80, nChars))
             line += 1
          Loop While line * 80 < sbString.Length
       End Sub
    End Module
    ' The example displays the following output:
    '    Aaaa_Bbbb_Cccc_Dddd_Eeee_Ffff_Gggg_Hhhh_Iiii_Jjjj_Kkkk_Llll_Mmmm_Nnnn_Oooo_Pppp_
    '    Qqqq_Rrrr_Ssss_Tttt_Uuuu_Vvvv_Wwww_Xxxx_Yyyy_Zzzz
    
  • 將所有未修改的文字儲存在 StringBuilder 物件中,呼叫 StringBuilder.ToString 方法將物件 String 轉換成 StringBuilder 物件,並在 物件上 String 執行修改。 如果您只有一些修改,則可以使用此方法;否則,使用不可變字串的成本可能會否定使用 StringBuilder 物件的效能優勢。

    下列範例在功能上與前兩個範例相同,但實作不同。 它會建立 StringBuilder 物件、將它 String 轉換成 物件,然後使用正則運算式對字串執行所有剩餘的修改。 方法 Regex.Replace(String, String, MatchEvaluator) 會使用 Lambda 運算式在每個相符專案上執行取代。

    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          // Create a StringBuilder object with 4 successive occurrences 
          // of each character in the English alphabet. 
          StringBuilder sb = new StringBuilder();
          for (ushort ctr = (ushort)'a'; ctr <= (ushort) 'z'; ctr++)
             sb.Append(Convert.ToChar(ctr), 4);
         
          // Convert it to a string.
          String sbString = sb.ToString();
    
          // Use a regex to uppercase the first occurrence of the sequence, 
          // and separate it from the previous sequence by an underscore.
          string pattern = @"(\w)(\1+)";
          sbString = Regex.Replace(sbString, pattern, 
                                   m => (m.Index > 0 ? "_" : "") + 
                                   m.Groups[1].Value.ToUpper() + 
                                   m.Groups[2].Value);
    
          // Display the resulting string.
          int line = 0;
          do {
             int nChars = line * 80 + 79 <= sbString.Length ? 
                                 80 : sbString.Length - line * 80;
             Console.WriteLine(sbString.Substring(line * 80, nChars));
             line++;
          } while (line * 80 < sbString.Length);
       }
    }
    // The example displays the following output:
    //    Aaaa_Bbbb_Cccc_Dddd_Eeee_Ffff_Gggg_Hhhh_Iiii_Jjjj_Kkkk_Llll_Mmmm_Nnnn_Oooo_Pppp_
    //    Qqqq_Rrrr_Ssss_Tttt_Uuuu_Vvvv_Wwww_Xxxx_Yyyy_Zzzz
    
    open System.Text
    open System.Text.RegularExpressions
    
    // Create a StringBuilder object with 4 successive occurrences
    // of each character in the English alphabet.
    let sb = StringBuilder()
    
    for char in 'a' .. 'z' do
        sb.Append(char, 4) |> ignore
    
    // Convert it to a string.
    let sbString = string sb
    
    // Use a regex to uppercase the first occurrence of the sequence,
    // and separate it from the previous sequence by an underscore.
    let pattern = @"(\w)(\1+)"
    
    let sbStringReplaced =
        Regex.Replace(
            sbString,
            pattern,
            fun m ->
                (if m.Index > 0 then "_" else "")
                + m.Groups[ 1 ].Value.ToUpper()
                + m.Groups[2].Value
        )
    
    // Display the resulting string.
    for line = 0 to (sbStringReplaced.Length - 1) / 80 do
        let nChars =
            if line * 80 + 79 <= sbStringReplaced.Length then
                80
            else
                sbStringReplaced.Length - line * 80
    
        printfn $"{sbStringReplaced.Substring(line * 80, nChars)}"
    
    // The example displays the following output:
    //    Aaaa_Bbbb_Cccc_Dddd_Eeee_Ffff_Gggg_Hhhh_Iiii_Jjjj_Kkkk_Llll_Mmmm_Nnnn_Oooo_Pppp_
    //    Qqqq_Rrrr_Ssss_Tttt_Uuuu_Vvvv_Wwww_Xxxx_Yyyy_Zzzz
    
    Imports System.Text
    Imports System.Text.RegularExpressions
    
    Module Example
       Public Sub Main()
          ' Create a StringBuilder object with 4 successive occurrences 
          ' of each character in the English alphabet. 
          Dim sb As New StringBuilder()
          For ctr As UShort = AscW("a") To Ascw("z")
             sb.Append(ChrW(ctr), 4)
          Next    
          ' Convert it to a string.
          Dim sbString As String = sb.ToString()
    
          ' Use a regex to uppercase the first occurrence of the sequence, 
          ' and separate it from the previous sequence by an underscore.
          Dim pattern As String = "(\w)(\1+)"
          sbString = Regex.Replace(sbString, pattern, 
                                   Function(m) If(m.Index > 0,"_","") + 
                                               m.Groups(1).Value.ToUpper + 
                                               m.Groups(2).Value)
    
          ' Display the resulting string.
          Dim line As Integer = 0
          Do
             Dim nChars As Integer = If(line * 80 + 79 <= sbString.Length, 
                                        80, sbString.Length - line * 80)
             Console.WriteLine(sbString.Substring(line * 80, nChars))
             line += 1
          Loop While line * 80 < sbString.Length
       End Sub
    End Module
    ' The example displays the following output:
    '    Aaaa_Bbbb_Cccc_Dddd_Eeee_Ffff_Gggg_Hhhh_Iiii_Jjjj_Kkkk_Llll_Mmmm_Nnnn_Oooo_Pppp_
    '    Qqqq_Rrrr_Ssss_Tttt_Uuuu_Vvvv_Wwww_Xxxx_Yyyy_Zzzz
    

將 StringBuilder 物件轉換為字串

您必須先將 StringBuilder 物件轉換成 String 物件,才能將 StringBuilder 物件所代表的字串傳遞給具有 String 參數的方法,或在使用者介面中加以顯示。 您可以呼叫 StringBuilder.ToString 方法來執行此轉換。 如需圖例,請參閱上一個範例,其會呼叫 ToString 方法,將物件轉換成 StringBuilder 字串,以便將其傳遞至正則運算式方法。

給呼叫者的注意事項

在 .NET Core 和 .NET Framework 4.0 和更新版本中,當您藉由呼叫 StringBuilder(Int32, Int32) 建構函式來具現化 StringBuilder 物件時,實例的 StringBuilder 長度和容量都可以成長超過其 MaxCapacity 屬性的值。 特別是當您呼叫 Append(String)AppendFormat(String, Object) 方法以附加小型字串時,就會發生此情況。

建構函式

StringBuilder()

初始化 StringBuilder 類別的新執行個體。

StringBuilder(Int32)

使用指定的容量來初始化 StringBuilder 類別的新執行個體。

StringBuilder(Int32, Int32)

初始化 StringBuilder 類別的新執行個體,將從指定的容量開始並且可以增加至指定的最大值。

StringBuilder(String)

以指定的字串初始化 StringBuilder 類別的新執行個體。

StringBuilder(String, Int32)

使用指定的字串和容量,來初始化 StringBuilder 類別的新執行個體。

StringBuilder(String, Int32, Int32, Int32)

以指定的子字串和容量初始化 StringBuilder 類別的新執行個體。

屬性

Capacity

取得或設定由目前執行個體配置的記憶體可以包含的最大字元數。

Chars[Int32]

取得或設定這個執行個體中指定字元位置的字元。

Length

取得或設定目前 StringBuilder 物件的長度。

MaxCapacity

取得這個執行個體的最大容量。

方法

Append(Boolean)

將指定的布林 (Boolean) 值之字串表示附加至這個執行個體。

Append(Byte)

將指定的 8 位元不帶正負號的整數之字串表示附加至這個執行個體。

Append(Char)

將指定 Char 物件的字串表示附加至這個執行個體。

Append(Char*, Int32)

將指定位址開頭的 Unicode 字元陣列附加至這個執行個體。

Append(Char, Int32)

將 Unicode 字元其字串表示的指定複本數附加至這個執行個體。

Append(Char[])

將指定陣列中的 Unicode 字元的字串表示附加至這個執行個體。

Append(Char[], Int32, Int32)

將 Unicode 字元之指定子陣列的字串表示附加至這個執行個體。

Append(Decimal)

將指定的小數位數之字串表示附加至這個執行個體。

Append(Double)

將指定的雙精度浮點數之字串表示附加至這個執行個體。

Append(IFormatProvider, StringBuilder+AppendInterpolatedStringHandler)

使用指定的格式,將指定的插入字串附加至這個實例。

Append(Int16)

將指定的 16 位元帶正負號的整數之字串表示附加至這個執行個體。

Append(Int32)

將指定的 32 位元帶正負號的整數之字串表示附加至這個執行個體。

Append(Int64)

將指定的 64 位元帶正負號的整數之字串表示附加至這個執行個體。

Append(Object)

將指定物件的字串表示附加至這個執行個體。

Append(ReadOnlyMemory<Char>)

將所指定唯讀字元記憶體區域的字串表示附加至這個執行個體。

Append(ReadOnlySpan<Char>)

將所指定唯讀字元範圍的字串表示附加至這個執行個體。

Append(SByte)

將指定的 8 位元帶正負號的整數之字串表示附加至這個執行個體。

Append(Single)

將指定的單精確度浮點數之字串表示附加至這個執行個體。

Append(String)

將指定字串的複本附加至這個執行個體。

Append(String, Int32, Int32)

將指定子字串的複本附加至這個執行個體。

Append(StringBuilder)

將指定字串產生器的字串表示附加至這個執行個體。

Append(StringBuilder, Int32, Int32)

將所指定字串產生器內的子字串複本附加至這個執行個體。

Append(StringBuilder+AppendInterpolatedStringHandler)

將指定的插入字串附加至這個實例。

Append(UInt16)

將指定的 16 位元不帶正負號的整數之字串表示附加至這個執行個體。

Append(UInt32)

將指定的 32 位元不帶正負號的整數之字串表示附加至這個執行個體。

Append(UInt64)

將指定的 64 位元不帶正負號的整數之字串表示附加至這個執行個體。

AppendFormat(IFormatProvider, CompositeFormat, Object[])

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每個格式專案都會由任何使用指定格式提供者之引數的字串表示來取代。

AppendFormat(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每個格式專案都會由任何使用指定格式提供者之引數的字串表示來取代。

AppendFormat(IFormatProvider, String, Object)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 使用指定的格式提供者,將每個格式項目取代為單一引數的字串表示。

AppendFormat(IFormatProvider, String, Object, Object)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 使用指定的格式提供者,將每個格式項目取代為兩個引數中的其中一個字串表示。

AppendFormat(IFormatProvider, String, Object, Object, Object)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 使用指定的格式提供者,將每個格式項目取代為三個引數中的其中一個字串表示。

AppendFormat(IFormatProvider, String, Object[])

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每一個格式項目會由參數陣列 (此參數陣列使用所指定的格式提供者) 中對應之物件引數的字串表示所取代。

AppendFormat(String, Object)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每一個格式項目都會取代為單一引數的字串表示。

AppendFormat(String, Object, Object)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每一個格式項目都會取代為兩個引數中任一個的字串表示。

AppendFormat(String, Object, Object, Object)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每一個格式項目都會取代為三個引數中任一個的字串表示。

AppendFormat(String, Object[])

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每一個格式項目會由參數陣列中對應之引數的字串表示所取代。

AppendFormat<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每個格式專案都會由任何使用指定格式提供者之引數的字串表示來取代。

AppendFormat<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每個格式專案都會由任何使用指定格式提供者之引數的字串表示來取代。

AppendFormat<TArg0>(IFormatProvider, CompositeFormat, TArg0)

將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每個格式專案都會由任何使用指定格式提供者之引數的字串表示來取代。

AppendJoin(Char, Object[])

在每個成員之間使用指定的字元分隔符號,串連在所提供物件陣列中表示項目的字串,然後將結果附加至字串產生器目前的執行個體。

AppendJoin(Char, String[])

在每個字串之間使用指定的字元分隔符號,串連所提供陣列的字串,然後將結果附加至字串產生器目前的執行個體。

AppendJoin(String, Object[])

在每個成員之間使用指定的分隔符號,串連在所提供物件陣列中表示項目的字串,然後將結果附加至字串產生器目前的執行個體。

AppendJoin(String, String[])

在每個字串之間使用指定的分隔符號,串連所提供陣列的字串,然後將結果附加至字串產生器目前的執行個體。

AppendJoin<T>(Char, IEnumerable<T>)

在每個成員之間使用指定的字元分隔符號,串連及附加集合的成員。

AppendJoin<T>(String, IEnumerable<T>)

在每個成員之間使用指定的分隔符號,以串連及附加集合的成員。

AppendLine()

將預設行結束字元附加至目前 StringBuilder 物件的尾端。

AppendLine(IFormatProvider, StringBuilder+AppendInterpolatedStringHandler)

使用指定的格式,後面接著預設行結束字元,將指定的插入字串附加至目前 StringBuilder 物件的結尾。

AppendLine(String)

將後面接著預設行結束字元的指定字串複本附加至目前的 StringBuilder 物件結尾。

AppendLine(StringBuilder+AppendInterpolatedStringHandler)

將指定的插入字串附加至目前 StringBuilder 物件的結尾,後面接著預設行結束字元。

Clear()

從目前的 StringBuilder 執行個體移除所有字元。

CopyTo(Int32, Char[], Int32, Int32)

將此執行個體指定區段中的字元複製到目的端 Char 陣列的指定區段。

CopyTo(Int32, Span<Char>, Int32)

將此執行個體指定區段中的字元複製到目的地 Char 範圍。

EnsureCapacity(Int32)

請確定這個 StringBuilder 執行個體的容量至少是某一指定的值。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Equals(ReadOnlySpan<Char>)

傳回值,指出此執行個體中的字元是否等於指定唯讀字元範圍的字元。

Equals(StringBuilder)

傳回值,該值表示這個執行個體是否和指定的物件相等。

GetChunks()

傳回物件,可用來逐一查看從這個 StringBuilder 執行個體建立的 ReadOnlyMemory<Char> 中所表示字元區塊。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Insert(Int32, Boolean)

在指定的字元位置上將 Boolean 值的字串表示插入這個執行個體。

Insert(Int32, Byte)

在指定的字元位置上將指定的 8 位元不帶正負號的整數之字串表示插入這個執行個體。

Insert(Int32, Char)

在指定的字元位置上將指定的 Unicode 字元之字串表示插入這個執行個體。

Insert(Int32, Char[])

在指定的字元位置上將指定的 Unicode 字元陣列之字串表示插入這個執行個體。

Insert(Int32, Char[], Int32, Int32)

在指定的字元位置上將 Unicode 字元之指定子陣列的字串表示插入這個執行個體。

Insert(Int32, Decimal)

在指定的字元位置上將小數位數的字串表示插入這個執行個體。

Insert(Int32, Double)

在指定的字元位置上將雙精度浮點數的字串表示插入這個執行個體。

Insert(Int32, Int16)

在指定的字元位置,將所指定帶正負號之 16 位元整數的字串表示插入這個執行個體。

Insert(Int32, Int32)

在指定的字元位置,將所指定帶正負號之 32 位元整數的字串表示插入這個執行個體。

Insert(Int32, Int64)

在指定的字元位置上將指定的 64 位元帶正負號的整數之字串表示插入這個執行個體。

Insert(Int32, Object)

在指定的字元位置上將物件的字串表示插入這個執行個體。

Insert(Int32, ReadOnlySpan<Char>)

將字元序列插入此執行個體的指定字元位置。

Insert(Int32, SByte)

在指定的字元位置,將所指定帶正負號之 8 位元整數的字串表示插入這個執行個體。

Insert(Int32, Single)

在指定的字元位置上將單精確度浮點數的字串表示插入這個執行個體。

Insert(Int32, String)

在指定的字元位置上將字串插入這個執行個體。

Insert(Int32, String, Int32)

在指定的字元位置上將指定字串的一或多個複本插入這個執行個體。

Insert(Int32, UInt16)

在指定的字元位置,將所指定不帶正負號的 16 位元整數之字串表示插入這個執行個體。

Insert(Int32, UInt32)

在指定的字元位置,將所指定不帶正負號的 32 位元整數之字串表示插入這個執行個體。

Insert(Int32, UInt64)

在指定的字元位置,將所指定不帶正負號的 64 位元整數之字串表示插入這個執行個體。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(Int32, Int32)

從這個執行個體移除指定的字元範圍。

Replace(Char, Char)

以另一個指定的字元,取代這個執行個體中指定字元的所有項目。

Replace(Char, Char, Int32, Int32)

將這個執行個體的子字串內所有出現的指定字元,取代為另一個指定的字元。

Replace(String, String)

將這個執行個體中所有出現的指定字串取代為另一個指定字串。

Replace(String, String, Int32, Int32)

將這個執行個體的子字串內所有出現的指定字串,取代為另一個指定的字串。

ToString()

將這個執行個體的值轉換為 String

ToString(Int32, Int32)

將這個執行個體的子字串值轉換為 String

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

將還原序列化目前 SerializationInfo 物件所需的資料填入 (Populate) StringBuilder 物件。

適用於

另請參閱