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) |
向此实例追加指定的布尔值的字符串表示形式。 |
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) |
使用所需的数据填充 SerializationInfo 对象来反序列化当前 StringBuilder 对象。 |
扩展方法
Append(StringBuilder, StringSegment) |
将指定的 StringSegment 添加到 StringBuilder。 |
AppendRedacted(StringBuilder, Redactor, ReadOnlySpan<Char>) |
编修潜在的敏感数据并将其追加到 StringBuilder 实例。 |
AppendRedacted(StringBuilder, Redactor, String) |
编修潜在的敏感数据并将其追加到 StringBuilder 实例。 |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈