C# String Replace() Method (original) (raw)

Last Updated : 11 Mar, 2025

In C#, the **Replace() method is used to replace all occurrences of a specified substring or character in a string with another substring or character. This method is particularly useful for string manipulation tasks, such as formatting or cleaning up data.

Example 1: Using Replace() method to replace "World" with "Geeks" in a string.

C# `

// C# program to illustrate // the use of Replace() Method using System;

class Geeks { public static void Main() { // String initialization and Declaration string s = "Hello, World";

    // Before Replacing 
    Console.WriteLine("Before Replacing: " + s);

    // Using Replace() method
    s = s.Replace("World", "Geeks");

    // After Replacing
    Console.WriteLine("After Replacing: " + s);		
} 

}

`

Output

Before Replacing: Hello, World After Replacing: Hello, Geeks

**Explanation: In the above example, we use a Replace() method of the String class to replace the **World with **Geeks.

**Syntax of String Replace() Method

public string Replace(char oldChar, char newChar)
public string Replace(string oldValue, string newValue)

**Parameters:

**Return Type:

**Exceptions:

**Example 2: Using the Replace() method to **replace all occurrences of a specified substring with a new substring.

C# `

// Using Replace(String oldValue, String newValue) method using System;

class Geeks { static void Main(string[] args) { string s = "Hello, Geek"; string res = s.Replace("Geek", "Geeks"); Console.WriteLine("" + res); } }

`

**Explanation: In the above example, the Replace() method replaces "Geek" with "Geeks" in the original string and returns a new string.

**Example 3: Using the Replace() method **to replace the specified character from the string.

C# `

// Using the Replace() method to replace the // specified character from the string using System;

class Geeks { static void Main(string[] args) { string s = "Hello, World!";

    string res = s.Replace('o', 'a');
    Console.WriteLine("Modified String: " + res);
}

}

`

Output

Modified String: Hella, Warld!

**Explanation: In the above example, we use the Replace() method which replaces all occurrences of a specified character with a new character. As shown the character 'o' replaces with 'a'.

**Example 4: Using the Replace() method in a Case-Sensitive Manner.

C# `

// Using Replace() method with Case Sensitive using System; class Geeks { static void Main(string[] args) {

    string s = "Hello, World!";
    string res = s.Replace("hello", "Geeks");
    Console.WriteLine("" + res);
}

}

`

**Explanation: In the above example, the string value passed as "hello" (lowercase) is not found in the original string, so no replacement were done because it is cases case-sensitive operation.

**Example 5: Performing multiple replacement operations on the String (Replacement’s Chain) using the Replace() method.

C# `

// Perfroming Multiple Replacements // at the same time using System;

class Geeks { static void Main(string[] args) { string s = "Hello, World! Welcome to the World of C#!";

    string res = s.Replace("World", "Geeks").Replace("C#", "C Sharp");

    Console.WriteLine("Modified String: " + res);
}

}

`

Output

Modified String: Hello, Geeks! Welcome to the Geeks of C Sharp!

**Explanation: In the above example, the multiple replacement operation is performed using the Replace() method of string class the world "World" with "Geeks" and "C#" with "C Sharp" using method chaining.