StringBuilder 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示可变字符字符串。 无法继承此类。
public ref class StringBuilder sealedpublic ref class StringBuilder sealed : System::Runtime::Serialization::ISerializablepublic sealed class StringBuilderpublic 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.ISerializabletype StringBuilder = classtype StringBuilder = class
    interface ISerializable[<System.Serializable>]
type StringBuilder = class[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StringBuilder = class
    interface ISerializablePublic NotInheritable Class StringBuilderPublic 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) | 将指定布尔值的字符串表示形式追加到此实例。 | 
| 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) | 将布尔值的字符串表示形式插入到此实例中的指定字符位置。 | 
| 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) | 使用反序列化当前 StringBuilder 对象所需的数据填充 SerializationInfo 对象。 | 
扩展方法
| Append(StringBuilder, StringSegment) | 将给定 StringSegment 添加到 StringBuilder。 | 
| AppendRedacted(StringBuilder, Redactor, ReadOnlySpan<Char>) | 对潜在的敏感数据进行修订,并将其追加到 StringBuilder 实例。 | 
| AppendRedacted(StringBuilder, Redactor, String) | 对潜在的敏感数据进行修订,并将其追加到 StringBuilder 实例。 |