使用StringBuilder替换字符串
设置一个字符串-
StringBuilder str =新字符串Builder("Fitness is important");
使用Replace()
方法替换字符串-
str.Replace("important", "essential");
以下是使用StringBuilder替换字符串的代码-
示例
using System; using System.Text; class Demo { static void Main() { //初始字符串 StringBuilder str =新字符串Builder("Fitness is important"); Console.WriteLine(str.ToString()); //替换 str.Replace("important", "essential"); //新字符串 Console.WriteLine(str.ToString()); Console.ReadLine(); } }
输出结果
Fitness is important Fitness is essential