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”
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
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:
String after replacement: The C Sharp tutorial on string replace!
What if multiple occurrences exist in the source string?
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:
String after replacement: C Sharp is Cool. C Sharp is awesome!
Is the Replace method 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:
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.
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:
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”.