HOWTO · Csharp

How to Remove a Character From a String in C#

Learn how to remove one character or a selected set from a C# string with string.Replace(), string.Split() and string.Join(), Regex.Replace(), or LINQ.

On this page

To remove one known character from a C# string, call string.Replace() with that character and string.Empty. To remove several selected characters, repeat that replacement for each character. Strings are immutable, so each call returns a new string; assign the result if you want to keep the change. Choose string.Split() with string.Join() when the characters are delimiters, Regex.Replace() for a character pattern, or LINQ when the rule describes which characters to keep. For removing whitespace specifically, see how to remove whitespaces from a string in C#.

Remove Characters With string.Replace()

string.Replace() replaces every occurrence of a specified character or substring. Replacing a character with string.Empty removes its occurrences. The following example applies that operation to each item in a selected set; the returned value from each call becomes the input to the next call.

Run this example:

using System;

class Program
{
    static void Main()
    {
        string text = "Something @to ,Write.;';";
        char[] charactersToRemove = { '@', ',', '.', ';', '\'' };

        foreach (char character in charactersToRemove)
        {
            text = text.Replace(character.ToString(), string.Empty);
        }

        Console.WriteLine(text);
    }
}
Something to Write

For a single character, the same idea is one call, such as text = text.Replace("@", string.Empty);. The method removes all matches, not only the first. If the selected set is large, repeated calls rescan and create intermediate strings; consider a character filter or a regular expression when its rule is clearer.

Remove Delimiters With string.Split() and string.Join()

string.Split() treats each specified character as a delimiter and returns the substrings between delimiters. string.Join() concatenates those parts using the separator you provide. An empty separator puts the remaining parts together directly, removing the delimiters. This is another way to express the operation; it is not inherently better than string.Replace().

using System;

class Program
{
    static void Main()
    {
        string text = "Something @to ,Write.;';";
        char[] delimiters = { '@', ',', '.', ';', '\'' };
        string result = string.Join(string.Empty,
            text.Split(delimiters, StringSplitOptions.RemoveEmptyEntries));

        Console.WriteLine(result);
    }
}
Something to Write

StringSplitOptions.RemoveEmptyEntries discards empty pieces that can occur when delimiters are adjacent or at the beginning or end. For the shown input, joining the split parts yields the same visible text as the replacement loop. Use this approach when splitting into pieces is useful in its own right; for simple character deletion, string.Replace() is more direct.

Remove Characters With Regex.Replace()

Regex.Replace() replaces every substring matched by a regular-expression pattern. A character class such as [@,.;'] matches any one of the listed characters, so replacing matches with string.Empty removes that selected set. Characters such as . have special meaning in regular expressions outside a character class, but here the period is literal.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string text = "Something @to ,Write.;';";
        string result = Regex.Replace(text, "[@,.;']", string.Empty);

        Console.WriteLine(result);
    }
}
Something to Write

A regular expression is useful when the removal rule is a pattern, such as a range or category of characters, rather than a short fixed list. Escape regular-expression metacharacters when they must match literally outside a character class. For a fixed list, string.Replace() or string.Split() can make the intended characters easier to see.

Filter Characters With LINQ

LINQ can construct a new string from characters that satisfy a condition. This example keeps whitespace, letters, and digits, so punctuation is removed. That is a property-based filter, not removal of an arbitrary caller-selected set: it keeps every character matching the rule, including letters and spaces.

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string text = "Something @to ,Write.;';";
        string result = new string(text
            .Where(character => char.IsWhiteSpace(character)
                || char.IsLetterOrDigit(character))
            .ToArray());

        Console.WriteLine(result);
    }
}
Something to Write

Use LINQ when the condition for keeping characters is the clearest way to state the requirement. Replace char.IsWhiteSpace and char.IsLetterOrDigit with a predicate that matches your policy if you need a different set. For a caller-provided list of exact characters, use the replacement loop or a corresponding membership test instead.