How to Remove a list of characters from a string in C#

By FoxLearn 2/5/2025 9:14:48 AM   3
When you want to remove a list of characters from a string, you can loop through the list and use string.Replace():
var removalList = new List<char> { 'a', 'i', 'u' };
var input = "Rain in the Jungle";
var cleanedInput = input;

foreach (char c in removalList)
{
    cleanedInput = cleanedInput.Replace(c.ToString(), string.Empty);
}

Console.WriteLine($"Before: {input}");
Console.WriteLine($"After: {cleanedInput}");

In this example, we want to remove the vowels 'a', 'i', and 'u' from the string "Rain in the Jungle". We loop through each character in the removal list and replace it with an empty string.

The output will be:

Before: Rain in the Jungle
After: Rn n th Jngl

Note: string.Replace() returns a new string (because strings are immutable in C#).

Using Where(), ToArray(), and new string()

Another option for removing a list of characters is to use a Linq one-liner:

  1. Use Where() on the string to filter out the characters you don’t want. This returns an IEnumerable<char>.
  2. Use ToArray() to convert the result to a character array.
  3. Use new string() to convert the character array back to a string.
using System.Linq;

var removalList = new List<char> { 'a', 'i', 'u' };
var input = "Rain in the Jungle";

var cleanedInput = new string(input.Where(c => !removalList.Contains(c)).ToArray());

Console.WriteLine($"Before: {input}");
Console.WriteLine($"After: {cleanedInput}");

This will output the following:

Before: Rain in the Jungle
After: Rn n th Jngl

Performance: This approach is approximately twice as slow as using string.Replace() in a loop, but it’s a cleaner, more concise one-liner.

Using StringBuilder and Loop (Faster before .NET 6)

Before .NET 6, the fastest option was to loop through the string and use a StringBuilder to accumulate the characters to keep (i.e., not in the removal list). If you're working with a version prior to .NET 6, you can use this approach.

using System.Text;

var removalList = new List<char> { 'a', 'i', 'u' };

string input = "Rain in the Jungle";

var sb = new StringBuilder();

foreach (var c in input)
{
    if (!removalList.Contains(c))
        sb.Append(c);
}

string cleanedInput = sb.ToString();

Console.WriteLine($"Before: {input}");
Console.WriteLine($"After: {cleanedInput}");

This will output the following:

Before: Rain in the Jungle
After: Rn n th Jngl

Performance Comparison

Here are the results of a performance test for three methods of removing a list of characters from a string. The test ran 100k iterations, removing a list of 10 characters from a string containing 2.5k characters:

ApproachAvg (ms)Min (ms)Max (ms)
string.Replace() in a loop0.030.021.32
StringBuilder in a loop0.040.034.35
Linq Where() + new string() + ToArray()0.060.044.19
Regex0.090.0616.58

List<char> vs HashSet<char>

Interestingly, using a List<char> is faster than using a HashSet<char> in every approach I tested. For small lists (like 10-15 characters), the overhead of a HashSet doesn’t provide any significant advantage. However, as the number of characters grows, the HashSet approach would eventually outperform the List.