How to Remove a list of characters from a string in C#
By FoxLearn 2/5/2025 9:14:48 AM 3
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:
- Use
Where()
on the string to filter out the characters you don’t want. This returns anIEnumerable<char>
. - Use
ToArray()
to convert the result to a character array. - 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:
Approach | Avg (ms) | Min (ms) | Max (ms) |
---|---|---|---|
string.Replace() in a loop | 0.03 | 0.02 | 1.32 |
StringBuilder in a loop | 0.04 | 0.03 | 4.35 |
Linq Where() + new string() + ToArray() | 0.06 | 0.04 | 4.19 |
Regex | 0.09 | 0.06 | 16.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
.
- String to Byte Array Conversion in C#
- How to Trim a UTF-8 string to the specified number of bytes in C#
- How to Save a list of strings to a file in C#
- How to Convert string list to int list in C#
- How to Convert string list to float list in C#
- How to Check if a string contains any substring from a list in C#
- Find a character in a string in C#
- Remove non-alphanumeric characters from a string in C#