Open In App

String Replace() Method in C#

Last Updated : 10 Sep, 2025
Comments
Improve
Suggest changes
4 Likes
Like
Report

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 : Using Replace() method to replace "World" with "Geeks" in a string.

C#
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

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

Parameters:

  • oldChar: The character which is to be replaced.
  • newChar: The new character will be replaced with the old character.
  • oldValue: The old substring which is to be replaced.
  • newValue: New substring that will replace with the old value.

Return Type:

  • This method returns a new string (System.String) where all occurrences of the specified character or substring are replaced with the given value. The original string remains unchanged.

Exceptions:

  • ArgumentNullException: If oldValue is null.
  • ArgumentException: If oldValue is an empty string (" ").

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

C#
using System;

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

Output
Hello, Geeks

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

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

C#
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 3: Using the Replace() method in a Case-Sensitive Manner.

C#
using System;
class Geeks
{
    static void Main(string[] args) {
        
        string s = "Hello, World!";
        string res = s.Replace("hello", "Geeks");
        Console.WriteLine("" + res);
    }
}

Output
Hello, World!

Explanation: In the above example, the string value passed as "hello" (lowercase) is not found in the original string, so no replacement was done because the Replace() method is case-sensitive.

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

C#
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.

Suggested Quiz
4 Questions

What happens if the substring to be replaced in Replace() is not found in the original string?

  • A

    It throws an exception

  • B

    It returns null

  • C

    It returns the unchanged original string

  • D

    It trims the string automatically

Explanation:

If the target substring or character is not found, Replace() simply returns the original string without changes.

What will be the output of this code?

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


  • A

    Hello, Geeks!

  • B

    Geeks, World!

  • C

    Hello, World!

  • D

    Compilation error

Explanation:

The Replace() method is case-sensitive. Since "hello" (lowercase) does not match "Hello" (uppercase), no replacement occurs.

What is the correct behavior of the Replace() method in C#?

  • A

    It modifies the original string directly

  • B

    It replaces only the first occurrence of the substring

  • C

    It returns a new string with all occurrences replaced

  • D

    It throws an error if the substring does not exist

Explanation:


What will be the output of the following code?

Java
string s = "Hello, World!";
string res = s.Replace('o', 'a');
Console.WriteLine(res);


  • A

    Hello, Warld!

  • B

    Hella, Warld!

  • C

    Hella, World!

  • D

    Hello, World!

Explanation:


Image
Quiz Completed Successfully
Your Score :   2/4
Accuracy :  0%
Login to View Explanation
1/4 1/4 < Previous Next >

Article Tags :

Explore