Replace String in C#

Here are some examples on how to replace strings in C#:

  • Replace a String to other text with case insensitive in C#:

To replace a string with another string with case-insensitive matching, you can use the Replace method with StringComparison option:

string input = "Hello world!";
string replacement = "bye";
string result = input.Replace("HELLO", replacement, StringComparison.OrdinalIgnoreCase);

In this example, the string "Hello" is replaced with "bye" in the input string, regardless of the case.

  • Replace of a String in place using C#:

To replace a string in place, you can use the StringBuilder class:

StringBuilder sb = new StringBuilder("Hello world!");
sb.Replace("Hello", "Hi");
string result = sb.ToString();

In this example, the StringBuilder class is used to replace the string "Hello" with "Hi" in the sb object.

  • Replace the first searched instance of a String in C#:

To replace only the first occurrence of a string, you can use the Replace method with a limit parameter:

string input = "Hello world, Hello everyone!";
string replacement = "Hi";
string result = input.Replace("Hello", replacement, 1);

In this example, only the first occurrence of "Hello" is replaced with "Hi" in the input string.

  • Replace Line Breaks in a String to other text using C#:

To replace line breaks with other text, you can use the Replace method with the Environment.NewLine constant:

string input = "Hello" + Environment.NewLine + "world!";
string replacement = "<br/>";
string result = input.Replace(Environment.NewLine, replacement);

In this example, the Environment.NewLine constant is used to represent the line breaks in the input string, and they are replaced with the <br/> tag.

  • How to replace multiple line breaks with a single <br/> in C#:

To replace multiple line breaks with a single <br/> tag, you can use a regular expression:

string input = "Hello" + Environment.NewLine + Environment.NewLine + "world!";
string result = Regex.Replace(input, @"(\r\n|\r|\n)+", "<br/>");

In this example, the regular expression (\r\n|\r|\n)+ matches one or more occurrences of line breaks (\r\n, \r, or \n), and replaces them with a single <br/> tag.

  • How to replace 1 line break with 2 line breaks in C#:

To replace one line break with two line breaks, you can use the Replace method with a double line break string:

string input = "Hello" + Environment.NewLine + "world!";
string result = input.Replace(Environment.NewLine, Environment.NewLine + Environment.NewLine);

In this example, the Environment.NewLine constant is used to represent the line breaks in the input string, and each occurrence is replaced with two line breaks.

  • How to replace multiple spaces with a single space in C#:

To replace multiple spaces with a single space, you can use a regular expression:

string input = "Hello     world!";
string result = Regex.Replace(input, @"\s+", " ");

In this example, the regular expression \s+ matches one or more whitespace characters, and replaces them with a single space.

  1. C# replace string example:

    string input = "Replace this string";
    string oldString = "this";
    string newString = "that";
    string result = input.Replace(oldString, newString);
    // Result: "Replace that string"
    
  2. Replace substring in C# string:

    string input = "Replace this substring";
    string oldSubstring = "this";
    string newSubstring = "that";
    string result = input.Replace(oldSubstring, newSubstring);
    // Result: "Replace that substring"
    
  3. Replacing characters in C# string:

    string input = "Replace characters";
    char charToReplace = 'e';
    char replacementChar = '*';
    string result = input.Replace(charToReplace, replacementChar);
    // Result: "R*plac* charact*rs"
    
  4. C# replace multiple occurrences in string:

    string input = "Replace multiple multiple occurrences";
    string target = "multiple";
    string replacement = "single";
    string result = input.Replace(target, replacement);
    // Result: "Replace single single occurrences"
    
  5. Case-insensitive string replacement in C#:

    string input = "Replace case-insensitive";
    string target = "CASE";
    string replacement = "case";
    string result = input.Replace(target, replacement, StringComparison.OrdinalIgnoreCase);
    // Result: "Replace case-insensitive"
    
  6. C# regex replace string:

    using System.Text.RegularExpressions;
    
    string input = "Replace using regex";
    string pattern = "using";
    string replacement = "with";
    string result = Regex.Replace(input, pattern, replacement);
    // Result: "Replace with regex"
    
  7. Replace part of a string in C#:

    string input = "Replace part of this string";
    int startIndex = 12;
    int length = 4;
    string replacement = "portion";
    string result = input.Remove(startIndex, length).Insert(startIndex, replacement);
    // Result: "Replace part of portion string"
    
  8. In-place string replacement in C#: Replacing part of a string in-place using a StringBuilder.

    StringBuilder sb = new StringBuilder("Replace in-place");
    sb.Replace("in-place", "inline");
    string result = sb.ToString();
    // Result: "Replace inline"
    
  9. Conditional string replacement in C#: Replacing a substring conditionally.

    string input = "Replace conditionally";
    string target = "conditionally";
    string result = input.Contains(target) ? input.Replace(target, "conditionally replaced") : input;
    // Result: "Replace conditionally replaced"