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.
string clean = "a-b-c".Replace("-", ""); // "abc" string dots = "a-b-c".Replace('-', '.'); // "a.b.c"
Try it
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.
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
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
StringComparisoncompare character values exactly. PassStringComparison.OrdinalIgnoreCasefor case-insensitive matching of identifiers and file names. - No "first only" overload. Find the index with
IndexOfand splice with slicing, as shown above, or useRegex.Replacewith a count. - Many replacements. Chained
Replacecalls allocate a string each time. For large inputs or dozens of rules, use aStringBuilder(which has its ownReplace) or a singleRegex.Replacewith a callback. - Replacement order matters.
"a".Replace("a", "b").Replace("b", "c")givesc, because the second call sees the output of the first. - Removing text.
Replace(x, "")removes every occurrence ofx. To remove by position, useRemove. - Patterns need regex.
Replacematches literal text only. Wildcards, digits and word boundaries need regular expressions. - Line endings.
ReplaceLineEndings()normalizes\r\n,\n,\rand a few Unicode separators in one call.