C# String - Replace() Method



The C# String Replace() method is used to return a new string by replacing all occurrences of the specified unicode character or string in the current string with another specified character or string.

It's important to note that this method can be overloaded by passing different arguments to this method. We can use the overloaded method of Replace() in the examples below.

Syntax

Following is the syntax of the C# string Replace(Char) method −

public string Replace (char oldChar, char newChar);

Parameters

This method accepts the following parameters −

  • oldChar/oldValue: It can be either Unicode character or a string to be replaced.
  • newChar/newValue: This can be either a Unicode character or a string that replaces all occurrences of oldChar or newValue.

Return value

This method returns a string that is equal to the current string except for all instances of the old value replaced with the newValue. If the oldValue is not found in the current instance, the method returns the current instance unchanged.

Example 1: Using Replace (string oldValue, string? newValue)

The following basic example demonstrates how you can use the Replace method to correct a spelling error −

    
using System;
using System.Globalization;
class Program {
   public static void Main() {
      string errString = "This docment uses 3 other docments to docment the docmentation";
   
      Console.WriteLine($"The original string is:{Environment.NewLine}{errString}");
   
      // Correct the spelling of "document".
      string correctString = errString.Replace("docment", "document");
   
      Console.WriteLine($"After correcting the string, the result is:{Environment.NewLine}{correctString}");
   }
}

Output

Following is the output −

The original string is:
This docment uses 3 other docments to docment the docmentation
After correcting the string, the result is:
This document uses 3 other documents to document the documentation

Example 2: Using Replace(char oldChar, char newChar)

Let us look at another example of the Replace() method. We create a comma separated value list by substituting commas for the blanks between a series of numbers −

using System;
using System.Globalization;
class Program {
   public static void Main() {
      string str = "1 2 3 4 5 6 7 8 9";
      Console.WriteLine("Original string: " + $"\"{str}\"");
      
      // use Replace() method
      string csv_string = str.Replace(' ', ',');
      Console.WriteLine("CSV string: " +  $"\"{csv_string}\"");
   }
}

Output

Following is the output −

Original string: "1 2 3 4 5 6 7 8 9"
CSV string: "1,2,3,4,5,6,7,8,9"

Example 3: Replace Multiple Occurrences of a Character

In this example, we use the Replace() method to replace an old character having multiple occurrences with a new character −

using System;

class Program {
   public static void Main() {
      string text = "mississippi";
      string result = text.Replace('i', 'o');
      Console.WriteLine(result);
   }
}

Output

Following is the output −

mossossoppo

Example 4: Replace and Chain Operations

In this example, we use the Replace() method to replace multiple substrings in a string. It creates a new string where specific substrings are replaced with other substrings −

using System;
class Program {
   public static void Main() {
      string text = "hello world";
      string result = text.Replace("hello", "hi").Replace("world", "tutorialspoint");
      Console.WriteLine(result);
   }
}

Output

Following is the output −

hi tutorialspoint
csharp_strings.htm
Advertisements