What is C# String Replace method?

C# String replace method tutorial with examples

Purpose of String Replace method

The string replace method is used to return a new string after changing all occurrences of the given substring in the source string.

In C#, the string is immutable i.e. once created it cannot be changed. So, the original string remains the same after using the string replace method.

A simple example of the Replace method for this source string can be:

“This is C# tutorial”

string str_new = source_str.Replace(“C#”, “C Sharp”);

I will show you complete examples with variations for C# Replace method, first have a look at the syntax of using it.

Syntax of using the Replace method

public string Replace (string oldValue, string newValue);

An example of String Replace method

In this example, we have a source string that is declared and assigned a value.

This is followed by using the Replace method where we will replace the word “C#” with “C Sharp” for the demo:

using System;

class string_replace_demo

{
    static void Main()
    {
        string str_rep = "The C# tutorial on string replace!";

        Console.WriteLine("The source string: " + str_rep);
        string str_after = str_rep.Replace("C#", "C Sharp");
        Console.WriteLine("String after replacement: " + str_after);

        Console.ReadLine();
    }
}

The output of this code:

The source string: The C# tutorial on string replace!
String after replacement: The C Sharp tutorial on string replace!

What if multiple occurrences exist in the source string?

As mentioned earlier, the C# Replace method will change all the occurrences of the existing substring by the given substring.

To demonstrate that, I have used the word “C#” twice in the source string.

Then, I used the Replace method for replacing “C#” with “C Sharp”. Have a look at what we got as the output:

using System;

class string_replace_demo
{
    static void Main()
    {

        string str_rep_mutiple = "C# is Cool. C# is awesome!";
        Console.WriteLine("The source string: " + str_rep_mutiple);
        string str_after = str_rep_mutiple.Replace("C#", "C Sharp");
        Console.WriteLine("String after replacement: " + str_after);

        Console.ReadLine();
    }
}

The output:

The source string: C# is Cool. C# is awesome!
String after replacement: C Sharp is Cool. C Sharp is awesome!

Is the Replace method case-sensitive?

The Replace method is case sensitive.

That means “C#” and “c#” are taken differently if provided as the New_string in the Replace method.

To demonstrate that, I used the same words in the source string with different cases:

using System;

class string_replace_demo
{
    static void Main()

    {
        string str_rep_case = "C# replace is case sensitive. Love c#.";
        Console.WriteLine("Original: " + str_rep_case);
        string str_after_case = str_rep_case.Replace("C#", "C Sharp");
        Console.WriteLine("After: " + str_after_case);

        Console.ReadLine();
    }
}

The output:

Original: C# replace is case sensitive. Love c#.
After: C Sharp replace is case sensitive. Love c#.

You can see in the output graphic that only the first occurrence of “C#” is replaced by “C Sharp” whereas the second occurrence remains the same i.e. “c#”.

How to limit the number of replacements?

In certain scenarios, you may require changing only the first one or two or so occurrences of the existing term by a new term rather than replacing all.

Unfortunately, there is no straightforward method of limiting the number of replacements for the old_value to new_value in the source string as using the Replace method.

However, you may use different ways of limiting the replacements.

One of the ways is using the Regex.Replace method for limiting the number of replacements.

The code:

using System;

using System.Text.RegularExpressions;

class string_replace_demo
{
    static void Main()
    {
        string str_rep_limit = "C# Cool. Love C#. Awesone C#.";
        Console.WriteLine("Original: " + str_rep_limit);

        var regex_replace = new Regex(Regex.Escape("C#"));
        var newValue = regex_replace.Replace(str_rep_limit, "C Sharp", 1);

        Console.WriteLine("After: " + newValue);
        Console.ReadLine();
    }
}

The output:

c# string replace limit

You can see, the source string contains three occurrences of the word “C#”. By using the regex Replace method, we specified “1” as the third argument for limiting the number of replacements.

It started from the left and only changed the first occurrence of “C#” to “C Sharp”.

Learn more about the regex replace method here.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!