开放的编程资料库

当前位置:我爱分享网 > C#教程 > 正文

C# 字符串生成器

C#StringBuilder教程展示了如何在C#中使用StringBuilderString对象是不可变的;只能创建原始字符串的复制副本。当我们需要就地修改字符串时,我们使用StringBuilder

字符串生成器

StringBuilder是可变的字符序列。StringBuilder在我们想要就地修改C#字符串时使用。

StringBuilder具有诸如AppendInsertReplace等允许修改字符串的方法。

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);

该示例演示了StringStringBuilder之间的主要区别。

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);

该示例使用AppendAppendLine方法构建一个字符串。

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#教程。

未经允许不得转载:我爱分享网 » C# 字符串生成器

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏