C# String Replace()

The Replace() method in C# is used to replace all occurrences of a specified character or substring within a string with a new character or substring. It has several overloads that allow you to perform different types of replacements.

Here’s the basic syntax of the Replace() method:

string.Replace(oldValue, newValue);

The oldValue parameter represents the character or substring you want to replace, and the newValue parameter represents the new character or substring that will replace it. The method returns a new string with all occurrences of oldValue replaced by newValue.

Here’s an example that demonstrates the usage of Replace():

string originalString = "Hello, world!";
string replacedString = originalString.Replace("world", "C#");

Console.WriteLine(replacedString);

Output:

Hello, C#!

Parameter:

The Replace() method in C# has several overloads that accept different parameters. Let’s explore the various parameters and overloads of the Replace() method:

  1. Single character replacement:
string.Replace(char oldValue, char newValue)

This overload replaces all occurrences of the specified character oldValue with the specified character newValue in the string.

  1. Substring replacement:
string.Replace(string oldValue, string newValue)

This overload replaces all occurrences of the specified substring oldValue with the specified substring newValue in the string.

  1. Case-insensitive replacement:
string.Replace(string oldValue, string newValue, StringComparison comparisonType)

This overload allows you to perform a case-insensitive replacement. The comparisonType parameter specifies the comparison rules for the replacement operation, such as StringComparison.CurrentCultureIgnoreCase, StringComparison.OrdinalIgnoreCase, etc.

  1. Specifying the number of replacements:
string.Replace(string oldValue, string newValue, int count)

This overload replaces a specified number of occurrences (count) of the specified substring oldValue with the specified substring newValue in the string.

  1. Replacing characters in a range:
string.Replace(char oldValue, char newValue, int startIndex, int count)

This overload replaces a specified number of characters (count) starting from the specified startIndex with the specified character newValue.

It’s important to note that the Replace() method creates and returns a new string with the replacements; it does not modify the original string in-place.

You can choose the appropriate overload based on your specific requirements.

Return:

The Replace() method in C# returns a new string that contains the replacements made. It does not modify the original string in-place.

Here’s an example to demonstrate the return value of the Replace() method:

string originalString = "Hello, world!";
string replacedString = originalString.Replace("world", "C#");

Console.WriteLine(replacedString);

Output:

Hello, C#!

In the above example, the Replace() method replaces the substring “world” with “C#” in the originalString and returns a new string "Hello, C#!". The replacedString variable holds this new string, and when we print it to the console, we see the modified string with the replacement. The original string originalString remains unchanged.

C# String Replace() Method Example:

Certainly! Here’s an example that demonstrates the usage of the Replace() method in C#:

string originalString = "The quick brown fox jumps over the lazy dog.";

// Replace a character
string replacedChar = originalString.Replace('o', '*');
Console.WriteLine("Replaced Character: " + replacedChar);

// Replace a substring
string replacedSubstring = originalString.Replace("quick", "fast");
Console.WriteLine("Replaced Substring: " + replacedSubstring);

// Case-insensitive replacement
string caseInsensitiveReplace = originalString.Replace("THE", "A", StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Case-insensitive Replacement: " + caseInsensitiveReplace);

// Replacing a limited number of occurrences
string limitedReplace = originalString.Replace("o", "*", 2);
Console.WriteLine("Limited Replacement: " + limitedReplace);

Output:

Replaced Character: The quick br*wn f*x jumps *ver the lazy d*g.
Replaced Substring: The fast brown fox jumps over the lazy dog.
Case-insensitive Replacement: A quick brown fox jumps over the lazy dog.
Limited Replacement: The quick br*wn f*x jumps over the lazy dog.

In the above example, we have a string originalString that contains a sentence. We perform several replacements using different overloads of the Replace() method:

  1. We replace the character ‘o’ with ‘*’ using Replace(char oldValue, char newValue).
  2. We replace the substring “quick” with “fast” using Replace(string oldValue, string newValue).
  3. We perform a case-insensitive replacement, replacing “THE” with “A” using Replace(string oldValue, string newValue, StringComparison comparisonType).
  4. We replace a limited number of occurrences of ‘o’ with ‘*’ (in this case, only 2 occurrences) using Replace(string oldValue, string newValue, int count).

In each case, we store the modified string in a separate variable and print the result to the console.