C# String Replace() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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"
CallWhat it does
text.Replace('-', '_')Swap every '-' for '_'.
text.Replace("cat", "dog")Swap every "cat" for "dog" (case-sensitive).
text.Replace(" ", "")Delete every space.
text.Replace("cat", "dog", StringComparison.OrdinalIgnoreCase)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

ParameterDescription
oldChar / oldValueRequired. What to find. A string value must not be null or empty.
newChar / newValueRequired. What to put in each match. "" or null deletes the matches.
comparisonTypeOptional. Matching rules, such as OrdinalIgnoreCase.

Return Value

Return ValueDescription
stringA 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);
    }
}

How It Works

  1. All dashes at once. Both dashes become plus signs in a single call; no loop is needed.
  2. 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.
  3. Original intact. s is printed last and still shows a-b-c.
  4. 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));
    }
}

How It Works

  1. Exact letters only. The two-argument form matches character codes exactly, so only the lowercase cat is swapped.
  2. Capitals left behind. Cat and CAT differ in case, so they are not matches and stay as they are.
  3. Ignore case. The StringComparison.OrdinalIgnoreCase overload treats all three spellings as equal and swaps each one.
  4. 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.

C#
using System;

class Program {
    static void Main() {
        string phone = "(555) 123-4567";
        string digits = phone.Replace("(", "").Replace(")", "")
                             .Replace(" ", "").Replace("-", "");
        Console.WriteLine(digits);

        string template = "Hi {name}, welcome back!";
        Console.WriteLine(template.Replace("{name}", "Sam"));
    }
}

How It Works

  1. One symbol per call. Each Replace removes one kind of character, and its result feeds the next call.
  2. Four temporary strings. Every step builds a new string. That is fine for short text; for big loops, StringBuilder.Replace is cheaper.
  3. Only digits remain. After the brackets, space, and dash are gone, ten digits are left.
  4. Simple templates. The braces are ordinary characters here, so {name} is just text that Replace swaps for Sam.

Frequently Asked Questions

Yes. It replaces every match in one call. To replace only the first match, find it with IndexOf and rebuild the string with Remove and Insert.
Yes. Replace(string, string) compares exact characters. In newer .NET, pass StringComparison.OrdinalIgnoreCase as a third argument to ignore case.
Pass an empty string as the new value: text.Replace("-", ""). The char overload cannot delete, because a char cannot be empty.
Strings are immutable. Replace returns a new string, so assign it: text = text.Replace("a", "b");
Replace("", "x") throws ArgumentException, and a null old value throws ArgumentNullException.

Next: ReplaceLineEndings()

Turn every kind of line break into one style, or into any text you choose.

ReplaceLineEndings() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful