myStringBuilder.Capacity = 25
EnsureCapacity 方法可用于检查当前 StringBuilder 的容量。 如果容量大于传递的值,则不进行任何更改;但是,如果容量小于传递的值,则会更改当前的容量以使其与传递的值匹配。
也可以查看或设置 Length 属性。 如果将 Length 属性设置为大于 Capacity 属性的值,则自动将 Capacity 属性更改为与 Length 属性相同的值。 如果将 Length 属性设置为小于当前 StringBuilder 对象内的字符串长度的值,则会缩短该字符串。
修改 StringBuilder 字符串
下表列出了可用于修改 StringBuilder 内容的方法。
Append 方法可用于将对象的文本或字符串表示形式添加到当前 StringBuilder 表示的字符串末尾。 下面的示例将 StringBuilder 对象初始化为“Hello World”,然后将一些文本追加到该对象的末尾。 将根据需要自动分配空间。
StringBuilder^ myStringBuilder = gcnew StringBuilder("Hello World!");
myStringBuilder->Append(" What a beautiful day.");
Console::WriteLine(myStringBuilder);
// The example displays the following output:
// Hello World! What a beautiful day.
StringBuilder myStringBuilder = new StringBuilder("Hello World!");
myStringBuilder.Append(" What a beautiful day.");
Console.WriteLine(myStringBuilder);
// The example displays the following output:
// Hello World! What a beautiful day.
Dim myStringBuilder As New StringBuilder("Hello World!")
myStringBuilder.Append(" What a beautiful day.")
Console.WriteLine(myStringBuilder)
' The example displays the following output:
' Hello World! What a beautiful day.
StringBuilder.AppendFormat 方法将文本添加到 StringBuilder 对象末尾。 它调用要设置格式的一个或多个对象的 IFormattable 实现,支持复合格式功能(有关详细信息,请参阅复合格式)。 因此,它接受数字、日期和时间以及枚举值的标准格式字符串、数字以及日期和时间值的自定义格式字符串,以及为自定义类型定义的格式字符串。 (有关格式化的信息,请参阅格式设置类型。)此方法可用于自定义变量格式,并将这些值追加到 StringBuilder。 下面的示例使用 AppendFormat 方法,将格式为货币值的整数值添加到 StringBuilder 对象末尾。
int MyInt = 25;
StringBuilder^ myStringBuilder = gcnew StringBuilder("Your total is ");
myStringBuilder->AppendFormat("{0:C} ", MyInt);
Console::WriteLine(myStringBuilder);
// The example displays the following output:
// Your total is $25.00
int MyInt = 25;
StringBuilder myStringBuilder = new StringBuilder("Your total is ");
myStringBuilder.AppendFormat("{0:C} ", MyInt);
Console.WriteLine(myStringBuilder);
// The example displays the following output:
// Your total is $25.00
Dim MyInt As Integer = 25
Dim myStringBuilder As New StringBuilder("Your total is ")
myStringBuilder.AppendFormat("{0:C} ", MyInt)
Console.WriteLine(myStringBuilder)
' The example displays the following output:
' Your total is $25.00
Insert
Insert 方法将字符串或对象添加到当前 StringBuilder 对象中的指定位置。 下面的示例使用此方法,将字词插入 StringBuilder 对象的第六个位置。
StringBuilder^ myStringBuilder = gcnew StringBuilder("Hello World!");
myStringBuilder->Insert(6,"Beautiful ");
Console::WriteLine(myStringBuilder);
// The example displays the following output:
// Hello Beautiful World!
StringBuilder myStringBuilder = new StringBuilder("Hello World!");
myStringBuilder.Insert(6,"Beautiful ");
Console.WriteLine(myStringBuilder);
// The example displays the following output:
// Hello Beautiful World!
Dim myStringBuilder As New StringBuilder("Hello World!")
myStringBuilder.Insert(6, "Beautiful ")
Console.WriteLine(myStringBuilder)
' The example displays the following output:
' Hello Beautiful World!
可以使用 Remove方法,从当前 StringBuilder 对象中指定索引(从零开始编制)处开始删除指定数量的字符。 下面的示例使用 Remove 方法缩短 StringBuilder 对象。
StringBuilder^ myStringBuilder = gcnew StringBuilder("Hello World!");
myStringBuilder->Remove(5,7);
Console::WriteLine(myStringBuilder);
// The example displays the following output:
// Hello
StringBuilder myStringBuilder = new StringBuilder("Hello World!");
myStringBuilder.Remove(5,7);
Console.WriteLine(myStringBuilder);
// The example displays the following output:
// Hello
Dim myStringBuilder As New StringBuilder("Hello World!")
myStringBuilder.Remove(5, 7)
Console.WriteLine(myStringBuilder)
' The example displays the following output:
' Hello
Replace 方法可用于将 StringBuilder 对象内的字符替换为另一个指定的字符。 下面的示例使用 Replace 方法,在 StringBuilder 对象中搜索感叹号字符 (!) 的所有实例,并将它们替换为问号字符 (?)。
StringBuilder^ myStringBuilder = gcnew StringBuilder("Hello World!");
myStringBuilder->Replace('!', '?');
Console::WriteLine(myStringBuilder);
// The example displays the following output:
// Hello World?
StringBuilder myStringBuilder = new StringBuilder("Hello World!");
myStringBuilder.Replace('!', '?');
Console.WriteLine(myStringBuilder);
// The example displays the following output:
// Hello World?
Dim myStringBuilder As New StringBuilder("Hello World!")
myStringBuilder.Replace("!"c, "?"c)
Console.WriteLine(myStringBuilder)
' The example displays the following output:
' Hello World?
将 StringBuilder 对象转换为字符串
必须先将 StringBuilder 对象转换为 String 对象,然后才能将 StringBuilder 对象表示的字符串传递给包含 String 参数的方法,或在用户界面中显示它。 可通过调用 StringBuilder.ToString 方法来执行此转换。 下面的示例先调用许多 StringBuilder 方法,再调用 StringBuilder.ToString() 方法来显示字符串。
using System;
using System.Text;
public class Example
public static void Main()
StringBuilder sb = new StringBuilder();
bool flag = true;
string[] spellings = { "recieve", "receeve", "receive" };
sb.AppendFormat("Which of the following spellings is {0}:", flag);
sb.AppendLine();
for (int ctr = 0; ctr <= spellings.GetUpperBound(0); ctr++) {
sb.AppendFormat(" {0}. {1}", ctr, spellings[ctr]);
sb.AppendLine();
sb.AppendLine();
Console.WriteLine(sb.ToString());
// The example displays the following output:
// Which of the following spellings is True:
// 0. recieve
// 1. receeve
// 2. receive
Imports System.Text
Module Example
Public Sub Main()
Dim sb As New StringBuilder()
Dim flag As Boolean = True
Dim spellings() As String = {"recieve", "receeve", "receive"}
sb.AppendFormat("Which of the following spellings is {0}:", flag)
sb.AppendLine()
For ctr As Integer = 0 To spellings.GetUpperBound(0)
sb.AppendFormat(" {0}. {1}", ctr, spellings(ctr))
sb.AppendLine()
sb.AppendLine()
Console.WriteLine(sb.ToString())
End Sub
End Module
' The example displays the following output:
' Which of the following spellings is True:
' 0. recieve
' 1. receeve
' 2. receive
System.Text.StringBuilder
基本字符串操作
格式设置类型