StringBuilder 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表一串可變的字元串。 此類別無法獲得繼承。
public ref class StringBuilder sealed
public ref class StringBuilder sealed : System::Runtime::Serialization::ISerializable
public sealed class StringBuilder
[System.Serializable]
public sealed class StringBuilder
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
type StringBuilder = class
[<System.Serializable>]
type StringBuilder = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StringBuilder = class
interface ISerializable
type StringBuilder = class
interface ISerializable
Public NotInheritable Class StringBuilder
Public NotInheritable Class StringBuilder
Implements ISerializable
- 繼承
-
StringBuilder
- 屬性
- 實作
範例
以下範例展示了如何呼叫該類別定義的 StringBuilder 許多方法。
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
備註
欲了解更多關於此 API 的資訊,請參閱 StringBuilder 的補充 API 備註。
給呼叫者的注意事項
在 .NET Core 及 .NET Framework 4.0 及後續版本中,當你呼叫 StringBuilder(Int32, Int32) 建構子實例化 StringBuilder 物件時,StringBuilder 實例的長度與容量都可能超過其 MaxCapacity 屬性的值。 這種情況尤其可能發生在你呼叫 和 Append(String)AppendFormat(String, Object) 方法來附加小字串時。
建構函式
| 名稱 | Description |
|---|---|
| StringBuilder() |
初始化 StringBuilder 類別的新執行個體。 |
| StringBuilder(Int32, Int32) |
初始化一個以指定容量開始並可成長至指定最大值的新類別實例 StringBuilder 。 |
| StringBuilder(Int32) |
使用指定的容量初始化該類別的新實例 StringBuilder 。 |
| StringBuilder(String, Int32, Int32, Int32) |
從指定的子字串與容量初始化該類別的新實例 StringBuilder 。 |
| StringBuilder(String, Int32) |
使用指定的字串與容量初始化該類別的新實例 StringBuilder 。 |
| StringBuilder(String) |
使用指定的字串初始化該類別的新實例 StringBuilder 。 |
屬性
| 名稱 | Description |
|---|---|
| Capacity |
取得或設定目前實例可包含的最大字元數。 |
| Chars[Int32] |
在此實例中,取得或設定字元於指定字元位置。 |
| Length |
取得或設定當前 StringBuilder 物件的長度。 |
| MaxCapacity |
獲得此實例的最大容量。 |
方法
| 名稱 | Description |
|---|---|
| Append(Boolean) |
將指定布林值的字串表示附加於此實例。 |
| Append(Byte) |
在此實例中附加指定 8 位元無符號整數的字串表示。 |
| Append(Char, Int32) |
在此實例中附加指定數量的 Unicode 字串表示副本。 |
| Append(Char) |
將指定 Char 物件的字串表示附加於此實例。 |
| Append(Char[], Int32, Int32) |
將指定 Unicode 子陣列的字串表示附加到此實例。 |
| Append(Char[]) |
將指定陣列中 Unicode 字元的字串表示附加到此實例。 |
| Append(Char*, Int32) |
在此實例中附加一個從指定位址開始的 Unicode 字元陣列。 |
| Append(Decimal) |
在此實例中附加指定十進位數的字串表示。 |
| Append(Double) |
在此實例中附加指定的雙精度浮點數字串表示。 |
| Append(Int16) |
在此實例中附加指定16位元有符號整數的字串表示。 |
| Append(Int32) |
在此實例中附加指定 32 位元有符號整數的字串表示。 |
| Append(Int64) |
在此實例中附加指定 64 位元有符號整數的字串表示。 |
| Append(Object) |
將指定物件的字串表示附加於此實例。 |
| Append(ReadOnlySpan<Char>) |
在此實例中附加指定唯讀字元區間的字串表示。 |
| Append(SByte) |
在此實例中附加指定 8 位元有符號整數的字串表示。 |
| Append(Single) |
在此實例中附加指定的單精度浮點數字串表示。 |
| Append(String, Int32, Int32) |
在此實例中附加指定子字串的副本。 |
| Append(String) |
將指定字串的副本附加到此實例。 |
| Append(StringBuilder, Int32, Int32) |
在此實例中附加指定字串建構器中的子字串副本。 |
| Append(StringBuilder) |
將指定字串建構器的字串表示附加到此實例。 |
| Append(UInt16) |
在此實例中附加指定 16 位元無符號整數的字串表示。 |
| Append(UInt32) |
在此實例中附加指定 32 位元無符號整數的字串表示。 |
| Append(UInt64) |
在此實例中附加指定 64 位元無符號整數的字串表示。 |
| AppendFormat(IFormatProvider, String, Object, Object, Object) |
處理包含零個或多個格式項目的複合格式字串,將傳回的字串附加至這個實例。 每個格式項目都會由使用指定格式提供者之三個自變數之一的字串表示所取代。 |
| AppendFormat(IFormatProvider, String, Object, Object) |
處理包含零個或多個格式項目的複合格式字串,將傳回的字串附加至這個實例。 每個格式項目都會使用指定的格式提供者,由兩個自變數之一的字串表示所取代。 |
| AppendFormat(IFormatProvider, String, Object) |
處理包含零個或多個格式項目的複合格式字串,將傳回的字串附加至這個實例。 每個格式項目都會由使用指定格式提供者的單一自變數字串表示所取代。 |
| AppendFormat(IFormatProvider, String, Object[]) |
處理包含零個或多個格式項目的複合格式字串,將傳回的字串附加至這個實例。 每個格式項目都會使用指定的格式提供者,取代參數陣列中對應自變數的字串表示。 |
| AppendFormat(String, Object, Object, Object) |
處理包含零個或多個格式項目的複合格式字串,將傳回的字串附加至這個實例。 每個格式項目都會由三個自變數之一的字串表示所取代。 |
| AppendFormat(String, Object, Object) |
處理包含零個或多個格式項目的複合格式字串,將傳回的字串附加至這個實例。 每個格式項目都會由兩個自變數之一的字串表示所取代。 |
| AppendFormat(String, Object) |
處理包含零個或多個格式項目的複合格式字串,將傳回的字串附加至這個實例。 每個格式項目都會由單一自變數的字串表示所取代。 |
| AppendFormat(String, Object[]) |
處理包含零個或多個格式項目的複合格式字串,將傳回的字串附加至這個實例。 每個格式項目都會由參數陣列中對應自變數的字串表示所取代。 |
| 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(String) |
在當前 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) |
傳回值,指出這個實例是否等於指定的物件。 |
| GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| Insert(Int32, Boolean) |
在指定字元位置插入布林值的字串表示。 |
| Insert(Int32, Byte) |
在此實例中插入指定的 8 位元無符號整數字串表示,並指定字元位置。 |
| Insert(Int32, Char) |
將指定 Unicode 字元的字串表示插入此實例中指定的字元位置。 |
| Insert(Int32, Char[], Int32, Int32) |
將指定 Unicode 子陣列的字串表示插入此實例中指定的字元位置。 |
| Insert(Int32, Char[]) |
將指定 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, Int32) |
在此實例中插入一個或多個指定字串的副本,並位於指定字元位置。 |
| Insert(Int32, String) |
在指定的字元位置插入一個字串。 |
| Insert(Int32, UInt16) |
在此實例中插入 16 位元無符號整數的字串表示,位置為指定字元位置。 |
| Insert(Int32, UInt32) |
在此實例中插入一個 32 位元無符號整數的字串表示,位置為指定的字元位置。 |
| Insert(Int32, UInt64) |
在此實例中插入一個 64 位元無符號整數的字串表示,位置為指定的字元位置。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Remove(Int32, Int32) |
移除此實例中指定的字元範圍。 |
| Replace(Char, Char, Int32, Int32) |
在此實例的子字串中,將指定字元的所有出現替換為另一個指定字元。 |
| Replace(Char, Char) |
將該實例中指定字元的所有出現替換為另一個指定字元。 |
| Replace(String, String, Int32, Int32) |
在此實例的子字串中,將指定字串的所有出現替換為另一指定字串。 |
| Replace(String, String) |
將該實例中指定字串的所有出現位置替換為另一指定字串。 |
| ToString() |
將此實例 String的值轉換為 。 |
| ToString(Int32, Int32) |
將該實例 String的子字串值轉換為 。 |
明確介面實作
| 名稱 | Description |
|---|---|
| ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
將 SerializationInfo 物件填充以反序列化當前 StringBuilder 物件所需的資料。 |