Set a String −
StringBuilder str = new StringBuilder("Fitness is important");Use the Replace() method to replace a string −
str.Replace("important", "essential");The following is the code to replace a string using StringBuilder −
Example
using System;
using System.Text;
class Demo {
static void Main() {
// Initial String
StringBuilder str = new StringBuilder("Fitness is important");
Console.WriteLine(str.ToString());
// Replace
str.Replace("important", "essential");
// New String
Console.WriteLine(str.ToString());
Console.ReadLine();
}
}Output
Fitness is important Fitness is essential