C# StringBuilder
最后修改于 2025 年 5 月 14 日
本 C# StringBuilder 教程演示了如何使用 StringBuilder
高效地操作字符串。与 String
对象不同,String
对象是不可变的,需要创建修改后的副本,而 StringBuilder
允许就地字符串修改,从而减少内存开销。
StringBuilder
StringBuilder
表示可变的字符序列,使其成为需要频繁修改字符串的场景的理想选择。与不可变字符串不同,它允许高效的更改,而无需不必要的内存分配。
StringBuilder
的关键方法包括
Append
- 将文本添加到现有字符串。Insert
- 在指定位置插入文本。Replace
- 替换指定子字符串的出现。
C# StringBuilder 是可变的
C# String
是不可变的,而 StringBuilder
是可变的。
using System.Text; var word = "rock"; var word2 = word.Replace('r', 'd'); Console.WriteLine(word2); var builder = new StringBuilder("rock"); builder.Replace('r', 'd'); Console.WriteLine(builder);
该示例演示了 String
和 StringBuilder
之间的主要区别。
var word2 = word.Replace('r', 'd');
C# String
有一个 Replace
方法,但它不会修改原始字符串。 而是创建一个修改后的副本。
var builder = new StringBuilder("rock"); builder.Replace('r', 'd');
另一方面,StringBuilder
就地替换字符串。
Console.WriteLine(builder);
Console.WriteLine
方法调用 StringBuilder
的 ToString
方法,该方法将构建器的值转换为字符串。
$ dotnet run dock dock
C# StringBuilder Append 和 AppendLine
Append
方法将指定对象的字符串表示形式附加到构建器。 AppendLine
方法将默认行终止符附加到当前 StringBuilder
对象的末尾。
using System.Text; var builder= new StringBuilder("There are"); builder.Append(" "); builder.Append("three hawks "); builder.Append("in the sky"); builder.AppendLine(); builder.AppendLine("The weather is beautiful"); builder.Append("The flowers blossom"); Console.WriteLine(builder);
该示例使用 Append
和 AppendLine
方法构建字符串。
var builder= new StringBuilder("There are"); builder.Append(" "); builder.Append("three hawks "); builder.Append("in the sky"); builder.AppendLine();
在这里,我们向 StringBuilder
添加一个句子。
builder.AppendLine("The weather is beautiful");
我们可以一次添加一个句子和一个新行。
$ dotnet run There are three hawks in the sky The weather is beautiful The flowers blossom
C# StringBuilder AppendJoin
AppendJoin
方法连接提供的对象数组中元素的字符串表示形式,在每个成员之间使用指定的分隔符,并将结果附加到字符串构建器的当前实例。
using System.Text; var words = new string[] { "in", "the", "sky"}; var builder = new StringBuilder("There are"); builder.Append(" "); builder.Append("three hawks "); builder.AppendJoin(" ", words); builder.Append("."); Console.WriteLine(builder);
在该程序中,我们使用 StringBuilder
构建消息。 一些单词来自字符串数组。
$ dotnet run There are three hawks in the sky.
C# StringBuilder AppendFormat
AppendFormat
方法允许将格式化的字符串添加到 StringBuilder
。
using System.Text; string name = "Peter"; int age = 34; var builder = new StringBuilder(); builder.AppendFormat("{0} is {1} years old", name, age); Console.WriteLine(builder);
在此示例中,我们使用 AppendFormat
构建一个字符串。
$ dotnet run Peter is 34 years old
C# StringBuilder Insert
Insert
方法用于将字符串插入到构建器的指定位置。
using System.Text; var sentence = "There is a red fox in the forest."; var builder = new StringBuilder(sentence); builder.Insert(19, "and a wolf "); Console.WriteLine(builder);
该示例使用 Insert
方法将字符串插入到句子中。
$ dotnet run There is a red fox and a wolf in the forest.
C# StringBuilder Replace
Replace
方法使用指定的新字符串替换字符串构建器中的子字符串。
using System.Text; var sentence = "I saw a red fox running into the forest."; var builder = new StringBuilder(sentence); var term = "fox"; var newterm = "dog"; int idx = builder.ToString().IndexOf(term); int len = term.Length; builder.Replace(term, newterm, idx, idx + len); Console.WriteLine(builder);
该示例用 dog 替换单词 fox。
int idx = builder.ToString().IndexOf(term);
我们找到要替换的子字符串的起始索引。
int len = term.Length;
我们获取子字符串的长度。
builder.Replace(term, newterm, idx, idx + len);
我们调用 Replace
方法。 第一个参数是旧值,第二个参数是新值。 接下来的两个参数是起始索引和子字符串的长度。
$ dotnet run I saw a red dog running into the forest.
C# StringBuilder Remove
Remove
方法从此实例中删除指定的字符范围。
using System.Text; var sentence = "There is a red fox in the forest."; var builder = new StringBuilder(sentence); builder.Remove(11, 3); Console.WriteLine(builder); builder.Remove(11, 1); Console.WriteLine(builder);
该示例从字符串中删除一些字符。
builder.Remove(11, 3);
使用 Remove
方法,我们删除一个子字符串。
builder.Remove(11, 1);
我们删除另一个字符。
$ dotnet run There is a fox in the forest. There is a fox in the forest.
$ dotnet run apple, banana, cherry, date, fig, grape
C# StringBuilder Clear 和重用
Clear
方法从 StringBuilder
实例中删除所有字符,允许您重用同一个对象来构建新字符串。 这在需要在循环或方法中构造多个字符串的情况下非常有用,从而减少内存分配并提高性能。
using System.Text; var builder = new StringBuilder(); builder.Append("First sentence."); Console.WriteLine(builder); builder.Clear(); builder.Append("Second sentence, after clearing."); Console.WriteLine(builder);
在此示例中,StringBuilder
用于构建两个不同的字符串。 输出第一个字符串后,调用 Clear
以重置构建器,并构造一个新字符串。 这种方法比每次创建一个新的 StringBuilder
实例更有效。
$ dotnet run First sentence. Second sentence, after clearing.
来源
在本文中,我们使用了 C# StringBuilder。
作者
列出所有 C# 教程。