Replace a string using StringBuilder

The StringBuilder class in C# provides an efficient way to modify strings without creating new string objects. The Replace() method allows you to replace all occurrences of a specified string or character with another string or character directly within the StringBuilder object.

Syntax

Following is the syntax for the Replace() method in StringBuilder −

public StringBuilder Replace(string oldValue, string newValue)
public StringBuilder Replace(char oldChar, char newChar)
public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count)

Parameters

  • oldValue/oldChar − The string or character to be replaced.
  • newValue/newChar − The string or character to replace with.
  • startIndex − The position to start the replacement (optional).
  • count − The number of characters to examine for replacement (optional).

Return Value

The Replace() method returns a reference to the same StringBuilder instance with the replacements made, enabling method chaining.

Using StringBuilder Replace() with Strings

Here's how to replace a string within a StringBuilder object −

using System;
using System.Text;

class Demo {
   static void Main() {
      // Initial String
      StringBuilder str = new StringBuilder("Fitness is important");
      Console.WriteLine("Original: " + str.ToString());

      // Replace string
      str.Replace("important", "essential");

      // Display modified string
      Console.WriteLine("Modified: " + str.ToString());
   }
}

The output of the above code is −

Original: Fitness is important
Modified: Fitness is essential

Using StringBuilder Replace() with Characters

You can also replace individual characters within the StringBuilder −

using System;
using System.Text;

class Demo {
   static void Main() {
      StringBuilder str = new StringBuilder("Hello World");
      Console.WriteLine("Original: " + str.ToString());

      // Replace character 'o' with 'a'
      str.Replace('o', 'a');
      Console.WriteLine("After replacing 'o' with 'a': " + str.ToString());

      // Replace character 'l' with 'x'
      str.Replace('l', 'x');
      Console.WriteLine("After replacing 'l' with 'x': " + str.ToString());
   }
}

The output of the above code is −

Original: Hello World
After replacing 'o' with 'a': Hella Warld
After replacing 'l' with 'x': Hexx? Warxd

Using StringBuilder Replace() with Range

You can specify a range within the StringBuilder to perform replacements −

using System;
using System.Text;

class Demo {
   static void Main() {
      StringBuilder str = new StringBuilder("Programming is fun and programming is easy");
      Console.WriteLine("Original: " + str.ToString());

      // Replace only first occurrence of "programming" (case-sensitive)
      // Starting from index 0, examining 20 characters
      str.Replace("programming", "coding", 0, 20);
      Console.WriteLine("After range replacement: " + str.ToString());

      // Replace all remaining occurrences
      str.Replace("programming", "coding");
      Console.WriteLine("After full replacement: " + str.ToString());
   }
}

The output of the above code is −

Original: Programming is fun and programming is easy
After range replacement: Programming is fun and coding is easy
After full replacement: Programming is fun and coding is easy

Method Chaining with StringBuilder Replace()

Since Replace() returns the StringBuilder instance, you can chain multiple operations −

using System;
using System.Text;

class Demo {
   static void Main() {
      StringBuilder str = new StringBuilder("Hello World! This is a test.");
      Console.WriteLine("Original: " + str.ToString());

      // Chain multiple replace operations
      str.Replace("Hello", "Hi")
         .Replace("World", "Universe")
         .Replace("test", "demo");

      Console.WriteLine("After chained replacements: " + str.ToString());
   }
}

The output of the above code is −

Original: Hello World! This is a test.
After chained replacements: Hi Universe! This is a demo.

Conclusion

The StringBuilder.Replace() method provides an efficient way to replace strings and characters without creating new string objects. It supports replacing within specific ranges and enables method chaining for multiple operations, making it ideal for scenarios requiring multiple string modifications.

Updated on: 2026-03-17T07:04:35+05:30

875 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements