Replace a string using StringBuilder

CsharpProgrammingServer Side Programming

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

 Live Demo

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
raja
Published on 04-Sep-2018 07:37:46
Advertisements