Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

C# String.Replace() method

Reference for C# string.Replace(): replace characters or substrings, ignore case with StringComparison, handle empty and null arguments, and replace once.

Replace returns a new string in which every occurrence of a character or substring is swapped for another. Strings are immutable, so the original is never changed: forgetting to use the return value is the classic mistake with this method.

All occurrences are replaced, from left to right, without overlapping. The text that was inserted is not searched again, so replacing a with aa does not loop forever.

C#
string clean = "a-b-c".Replace("-", "");   // "abc"
string dots = "a-b-c".Replace('-', '.');   // "a.b.c"

Try it

C# Replace characters and substrings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Case, comparison and edge cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public string Replace(char oldChar, char newChar)
public string Replace(string oldValue, string? newValue)
public string Replace(string oldValue, string? newValue, StringComparison comparisonType)
public string Replace(string oldValue, string? newValue, bool ignoreCase, CultureInfo? culture)
Name Description
oldChar, oldValue What to find. Must not be null; the string version must not be empty.
newChar, newValue The replacement. A null newValue removes the matches.
comparisonType How to compare: ordinal, ordinal ignoring case, or culture-aware. See StringComparison.
ignoreCase, culture Older form of the same idea; null culture means the current culture.

Returns: a new string. If nothing matched, the same instance is returned.

Errors / undefined behavior: ArgumentNullException if oldValue is null. ArgumentException if oldValue is an empty string.

Usage notes

  • Immutable result. s.Replace(...) alone does nothing useful. Assign the result: s = s.Replace(...).
  • Ordinal and case-sensitive by default. The overloads without a StringComparison compare character values exactly. Pass StringComparison.OrdinalIgnoreCase for case-insensitive matching of identifiers and file names.
  • No "first only" overload. Find the index with IndexOf and splice with slicing, as shown above, or use Regex.Replace with a count.
  • Many replacements. Chained Replace calls allocate a string each time. For large inputs or dozens of rules, use a StringBuilder (which has its own Replace) or a single Regex.Replace with a callback.
  • Replacement order matters. "a".Replace("a", "b").Replace("b", "c") gives c, because the second call sees the output of the first.
  • Removing text. Replace(x, "") removes every occurrence of x. To remove by position, use Remove.
  • Patterns need regex. Replace matches literal text only. Wildcards, digits and word boundaries need regular expressions.
  • Line endings. ReplaceLineEndings() normalizes \r\n, \n, \r and a few Unicode separators in one call.