StringBuilder 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示可變動的字元字串。 此類別無法獲得繼承。
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
備註
如需此 API 的詳細資訊,請參閱 StringBuilder 的補充 API 備註。
給呼叫者的注意事項
在 .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(IFormatProvider, String, ReadOnlySpan<Object>) |
表示可變動的字元字串。 此類別無法獲得繼承。 |
AppendFormat(String, Object) |
將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每一個格式項目都會取代為單一引數的字串表示。 |
AppendFormat(String, Object, Object) |
將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每一個格式項目都會取代為兩個引數中任一個的字串表示。 |
AppendFormat(String, Object, Object, Object) |
將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每一個格式項目都會取代為三個引數中任一個的字串表示。 |
AppendFormat(String, Object[]) |
將處理複合格式字串所傳回的字串 (其中包含零或更多的格式項目) 附加至這個執行個體。 每一個格式項目會由參數陣列中對應之引數的字串表示所取代。 |
AppendFormat(String, ReadOnlySpan<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, ReadOnlySpan<Object>) |
表示可變動的字元字串。 此類別無法獲得繼承。 |
AppendJoin(Char, ReadOnlySpan<String>) |
表示可變動的字元字串。 此類別無法獲得繼承。 |
AppendJoin(Char, String[]) |
在每個字串之間使用指定的字元分隔符號,串連所提供陣列的字串,然後將結果附加至字串產生器目前的執行個體。 |
AppendJoin(String, Object[]) |
在每個成員之間使用指定的分隔符號,串連在所提供物件陣列中表示項目的字串,然後將結果附加至字串產生器目前的執行個體。 |
AppendJoin(String, ReadOnlySpan<Object>) |
表示可變動的字元字串。 此類別無法獲得繼承。 |
AppendJoin(String, ReadOnlySpan<String>) |
表示可變動的字元字串。 此類別無法獲得繼承。 |
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 執行個體建立的 |
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(ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
表示可變動的字元字串。 此類別無法獲得繼承。 |
Replace(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Int32, Int32) |
表示可變動的字元字串。 此類別無法獲得繼承。 |
Replace(String, String) |
將這個執行個體中所有出現的指定字串取代為另一個指定字串。 |
Replace(String, String, Int32, Int32) |
將這個執行個體的子字串內所有出現的指定字串,取代為另一個指定的字串。 |
ToString() |
將這個執行個體的值轉換為 String。 |
ToString(Int32, Int32) |
將這個執行個體的子字串值轉換為 String。 |
明確介面實作
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
將還原序列化目前 SerializationInfo 物件所需的資料填入 (Populate) StringBuilder 物件。 |
擴充方法
Append(StringBuilder, StringSegment) |
將指定的 StringSegment 新增到 StringBuilder。 |
AppendRedacted(StringBuilder, Redactor, ReadOnlySpan<Char>) |
修訂潛在的敏感數據,並將其附加至 StringBuilder 實例。 |
AppendRedacted(StringBuilder, Redactor, String) |
修訂潛在的敏感數據,並將其附加至 StringBuilder 實例。 |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應