StringBuilder 类

定义

表示可变字符字符串。 此类不能被继承。

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) 方法以追加小字符串时,会出现这种情况。

构造函数

名称 说明
StringBuilder()

初始化 StringBuilder 类的新实例。

StringBuilder(Int32, Int32)

初始化以指定容量开头的 StringBuilder 类的新实例,并可以增长到指定的最大值。

StringBuilder(Int32)

使用指定的容量初始化类的新实例 StringBuilder

StringBuilder(String, Int32, Int32, Int32)

从指定的子字符串和容量初始化类的新实例 StringBuilder

StringBuilder(String, Int32)

使用指定的字符串和容量初始化类的新实例 StringBuilder

StringBuilder(String)

使用指定的字符串初始化类的新实例 StringBuilder

属性

名称 说明
Capacity

获取或设置可以包含在当前实例分配的内存中的最大字符数。

Chars[Int32]

获取或设置此实例中指定字符位置的字符。

Length

获取或设置当前 StringBuilder 对象的长度。

MaxCapacity

获取此实例的最大容量。

方法

名称 说明
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

显式接口实现

名称 说明
ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用 SerializationInfo 反序列化当前 StringBuilder 对象所需的数据填充对象。

适用于

另请参阅