C# String Replace()

The Replace() method returns a new string by replacing each matching character/substring in the string with the new character/substring.

Example

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string str = "Programming is fun";
    
// replaces "Programming" with "C#" string result = str.Replace("Programming", "C#");
Console.WriteLine(result); Console.ReadLine(); } } } // Output: C# is fun

Replace() Syntax

The syntax of the string Replace() method is:

Replace(string oldValue, string newValue)

Here, Replace() is a method of class String.


Replace() Parameters

The Replace() method takes the following parameters:

  • oldValue - the substring that we want to replace
  • newValue - new substring which will replace the old substring

Replace() Return Value

  • The Replace() method returns a new string by replacing each matching character/substring in the string with the new character/substring.

Example 1: C# String Replace() Characters

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
 
      string str = "Bat";
      Console.WriteLine("Old value: " + str);
     
      string result;
 
// replaces 'B' with 'C'' result = str.Replace('B', 'C');
Console.WriteLine("New value: " + result); Console.ReadLine(); } } }

Output

Old value: Bat
New value: Cat

In the above example, the Replace() method replaces the character 'B' with 'C'.


Example 2: C# String Replace() Substrings

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
 
      string str = "C# Programming";     
      string result;
 
// replaces "C#" with "Java" result = str.Replace("C#", "Java");
Console.WriteLine("New Value1: " + result);
// returns initial string result = str.Replace("Go", "C++");
Console.WriteLine("New Value2: " + result); Console.ReadLine(); } } }

Output

New Value1: Java Programming
New Value2: C# Programming
  • str.Replace("C#", "Java") - replaces substring "C#" with string "Java"
  • str.Replace("Go", "C++") - returns the initial string because there is no "Go" substring

Example 3: Replace() Chain

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
 
      string str = "AAA";
      Console.WriteLine("Old Value: "+ str);
     
      string result;

// performs multiple replacement result = str.Replace("AAA", "BBB").Replace("BBB", "CCC");
Console.WriteLine("New Value: " + result); Console.ReadLine(); } } }

Output

Old Value: AAA
New Value: CCC

In the above example, we have performed multiple replacements in the initial string.

Method calls are executed left to right. So,

  • first, "AAA" will be replaced by "BBB"
  • then, "BBB" will be replaced by "CCC".

Hence, we get "CCC" as the output.

Note: The Replace() method does not modify the value of the current instance. Instead, it returns a new string by replacing the occurrence of old values with new values.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges