In C#, Replace() finds every copy of a character or substring and swaps it for new text. You do not need a loop; one call handles all matches. The result is a new string.
There are two main forms: Replace(char, char) swaps single characters, and Replace(string, string) swaps text. Pass an empty string as the new value to delete the matches. The string form is case-sensitive and compares exact characters (ordinal).
This is an instance member of System.String. Call it on a string variable, such as text.Replace(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: Replace() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable.
Syntax
Pass what to find first, and what to put in its place second.
C#
string s = "a-b-c";
string plus = s.Replace('-', '+'); // "a+b+c"
string none = s.Replace("-", ""); // "abc"
// s is still "a-b-c"
Ignore case while matching (newer .NET, .NET Core 2.0 and later).
The old value must not be null (ArgumentNullException) or empty (ArgumentException). A null new value is treated like "", which deletes the matches. If nothing matches, you get the same text back. Matches never overlap: "aaa".Replace("aa", "b") is "ba".
Library declarations
C#
public string Replace(char oldChar, char newChar);
public string Replace(string oldValue, string? newValue);
public string Replace(string oldValue, string? newValue, StringComparison comparisonType); // newer .NET
public string Replace(string oldValue, string? newValue, bool ignoreCase, CultureInfo? culture); // newer .NET
Parameters
Parameter
Description
oldChar / oldValue
Required. What to find. A string value must not be null or empty.
newChar / newValue
Required. What to put in each match. "" or null deletes the matches.
comparisonType
Optional. Matching rules, such as OrdinalIgnoreCase.
Return Value
Return Value
Description
string
A new string with all matches replaced, or the same text if nothing matched.
C# Examples
Example 1 — Replace Characters and Delete Them
Use the char form to swap dashes and the string form to delete them, then show the original is untouched.
C#
using System;
class Program {
static void Main() {
string s = "a-b-c";
Console.WriteLine(s.Replace('-', '+'));
Console.WriteLine(s.Replace("-", ""));
Console.WriteLine(s);
}
}
Output
a+b+c
abc
a-b-c
How It Works
All dashes at once. Both dashes become plus signs in a single call; no loop is needed.
Delete with an empty string.Replace("-", "") swaps each dash for nothing, joining the letters into abc. The char form cannot do this, because there is no empty char.
Original intact.s is printed last and still shows a-b-c.
Same length or shorter. The char swap keeps 5 characters; the delete makes the result 3 characters long.
Example 2 — Case-Sensitive vs Ignore-Case Replace
Swap the word cat in a sentence that spells it three different ways.
C#
using System;
class Program {
static void Main() {
string t = "Cat cat CAT";
Console.WriteLine(t.Replace("cat", "dog"));
Console.WriteLine(t.Replace("cat", "dog", StringComparison.OrdinalIgnoreCase));
}
}
Output
Cat dog CAT
dog dog dog
How It Works
Exact letters only. The two-argument form matches character codes exactly, so only the lowercase cat is swapped.
Capitals left behind.Cat and CAT differ in case, so they are not matches and stay as they are.
Ignore case. The StringComparison.OrdinalIgnoreCase overload treats all three spellings as equal and swaps each one.
Version note. The overload with StringComparison exists in newer .NET (.NET Core 2.0 and later). On .NET Framework you would use Regex.Replace with RegexOptions.IgnoreCase.
Example 3 — Clean a Phone Number and Fill a Template
Chain several Replace calls to strip symbols, and use one call to fill a placeholder.