C#StringBuilder教程展示了如何在C#中使用StringBuilder
。String
对象是不可变的;只能创建原始字符串的复制副本。当我们需要就地修改字符串时,我们使用StringBuilder
。
字符串生成器
StringBuilder
是可变的字符序列。StringBuilder
在我们想要就地修改C#字符串时使用。
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#StringBuilderAppend和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
方法使用每个成员之间指定的分隔符连接所提供对象数组中元素的字符串表示形式,并将结果附加到stringbuilder的当前实例。
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
方法允许将格式化字符串添加到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
方法用于将字符串插入构建器的指定位置。
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
方法用指定的新字符串替换字符串生成器中的子字符串。
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);
该示例将单词fox替换为dog。
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
方法从此实例中删除指定范围的字符。
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.
在本文中,我们使用了C#StringBuilder。
列出所有C#教程。