Table of contents
1.
Introduction
2.
Syntax
2.1.
Replacing Substrings
2.2.
Replacing Characters
3.
Exceptions
4.
Using the Replace() Method for Strings
4.1.
Example: Replacing Substrings
5.
Using the Replace() Method for Characters
5.1.
Example: Replacing Characters
6.
Using the Replace() Method in a Case-Sensitive Manner
6.1.
Example: Case-Sensitive Replacement
7.
Performing Multiple Replacement Operations on the String (Replacement’s Chain)
7.1.
Example: Chaining Replacements
8.
Frequently Asked Questions
8.1.
What does the Replace() method do in C#?
8.2.
Is the Replace() method case-sensitive?
8.3.
Can I perform multiple replacements in a single statement?
9.
Conclusion
Last Updated: Mar 11, 2025
Easy

C# String Replace()

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Replace() method in C# is used to replace all occurrences of a specified substring or character with another substring or character. Since strings in C# are this method returns a new modified string instead of changing the original one. It is commonly used in text manipulation, data formatting, and replacing unwanted characters in a string. 

C# String Replace()

In this article, you will learn how the Replace() method works with examples.

Syntax

The Replace() method in C# comes in two primary forms:

Replacing Substrings

public string Replace(string oldValue, string newValue)
  • oldValue: The substring you want to replace.
     
  • newValue: The substring to replace oldValue with.

Replacing Characters

public string Replace(char oldChar, char newChar)
  • oldChar: The character you want to replace.
     
  • newChar: The character to replace oldChar with.

Exceptions

While using the Replace() method, be mindful of the following exceptions:

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

Using the Replace() Method for Strings

The Replace() method is commonly used to substitute specific substrings within a string. This is particularly useful when you need to modify parts of a text based on certain criteria.

Example: Replacing Substrings

using System;
class Program
{
    static void Main()
    {
        string sentence = "Coding is fun and rewarding.";
        string modifiedSentence = sentence.Replace("fun", "challenging");
        Console.WriteLine(modifiedSentence);
    }
}

 

Output:

Coding is challenging and rewarding.

 

Explanation:

In this example, the word "fun" in the original sentence is replaced with "challenging" using the Replace() method. The modified sentence is then displayed.

Using the Replace() Method for Characters

The Replace() method can also be used to replace individual characters within a string. This is useful when you need to change specific characters without affecting the rest of the string.

Example: Replacing Characters

using System;
class Program
{
    static void Main()
    {
        string data = "1001-1002-1003";
        string modifiedData = data.Replace('-', ':');
        Console.WriteLine(modifiedData);
    }
}

 

Output:

1001:1002:1003

 

Explanation:

Here, the hyphens (-) in the original string are replaced with colons (:) using the Replace() method. The modified string is then displayed.

Using the Replace() Method in a Case-Sensitive Manner

The Replace() method in C# is case-sensitive by default. This means it distinguishes between uppercase and lowercase characters when performing replacements.

Example: Case-Sensitive Replacement

using System;
class Program
{
    static void Main()
    {
        string text = "Hello World! Welcome to the world of C#.";
        string modifiedText = text.Replace("world", "universe");
        Console.WriteLine(modifiedText);
    }
}

 

Output:

Hello World! Welcome to the universe of C#.

 

Explanation:

In this example, only the lowercase "world" is replaced with "universe". The uppercase "World" remains unchanged due to the case-sensitive nature of the Replace() method.

Performing Multiple Replacement Operations on the String (Replacement’s Chain)

You can chain multiple Replace() methods to perform several replacements in a single statement. This technique is useful when you need to make multiple modifications to a string.

Example: Chaining Replacements

using System;
class Program
{
    static void Main()
    {
        string phrase = "The quick brown fox jumps over the lazy dog.";
        string modifiedPhrase = phrase.Replace("fox", "cat").Replace("dog", "mouse");
        Console.WriteLine(modifiedPhrase);
    }
}

 

Output:

The quick brown cat jumps over the lazy mouse.

 

Explanation:

In this example, the word "fox" is first replaced with "cat", and then "dog" is replaced with "mouse" by chaining two Replace() methods. The final modified phrase is then displayed.

Frequently Asked Questions

What does the Replace() method do in C#?

The Replace() method substitutes all occurrences of a specified character or substring, within a string with a new character or substring.

Is the Replace() method case-sensitive?

Yes, the Replace() method is case-sensitive. It distinguishes between uppercase and lowercase characters during the replacement process.

Can I perform multiple replacements in a single statement?

Yes, by chaining multiple Replace() methods, you can perform several replacements sequentially in a single statement.

Conclusion

In this article, we explored the Replace() method in C#, which is used to replace specific characters or substring within a string. This method is useful for string manipulation, data formatting, and text processing. Understanding how to use Replace() effectively helps in writing cleaner and more efficient C# applications.

Live masterclass