Replace a String Using StringBuilder



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
Updated on: 2020-06-22T13:38:40+05:30

698 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements