Remove specific characters from a string in C#
Given a set of characters, remove those characters from a string in C#.
It is common knowledge that string is immutable in C#, i.e., we cannot change the contents of a string object; we can change only the reference to the object. Therefore, to remove any characters from a string, we need to create its new instance. This post provides an overview of several methods to accomplish this:
1. Using String.Replace() method
A fairly simple solution is to use the String.Replace() method for replacing all occurrences of specified characters in the current string. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.Collections.Generic; public static class Extensions { public static string Filter(this string str, List<char> charsToRemove) { foreach (char c in charsToRemove) { str = str.Replace(c.ToString(), String.Empty); } return str; } } public class Example { public static void Main() { string str = "@Hello, World."; List<char> charsToRemove = new List<char>() { '@' ,'_', ',' ,'.' }; str = str.Filter(charsToRemove); Console.WriteLine(str); } } /* Output: Hello World */ |
We can also use the List.ForEach() method in place of the foreach loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Collections.Generic; public static class Extensions { public static string Filter(this string str, List<char> charsToRemove) { charsToRemove.ForEach(c => str = str.Replace(c.ToString(), String.Empty)); return str; } } public class Example { public static void Main() { string str = "@Hello, World."; List<char> charsToRemove = new List<char>() { '@' ,'_', ',' ,'.' }; str = str.Filter(charsToRemove); Console.WriteLine(str); } } /* Output: Hello World */ |
2. Using String.Split() method
The idea is to split the string with given characters. This can be easily done using the String.Split() method, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Collections.Generic; public static class Extensions { public static string Filter(this string str, List<char> charsToRemove) { // return String.Join(String.Empty, str.Split(charsToRemove.ToArray())); return String.Concat(str.Split(charsToRemove.ToArray())); } } public class Example { public static void Main() { string str = "@Hello, World."; List<char> charsToRemove = new List<char>() { '@', '_', ',', '.' }; str = str.Filter(charsToRemove); Console.WriteLine(str); } } /* Output: Hello World */ |
3. Regular Expressions
We can use regular expressions to remove specific characters in a string. The idea is to check for specific characters in a string and replace them with an empty string. The following code example shows how to implement this. Note below implementation will fail for characters that need escaping.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Text.RegularExpressions; using System.Collections.Generic; public static class Extensions { public static string Filter(this string str, List<char> charsToRemove) { String chars = "[" + String.Concat(charsToRemove) + "]"; return Regex.Replace(str, chars, String.Empty); } } public class Example { public static void Main() { string str = "@Hello, World."; List<char> charsToRemove = new List<char>() { '@' ,'_', ',' ,'.' }; str = str.Filter(charsToRemove); Console.WriteLine(str); } } /* Output: Hello World */ |
That’s all about removing specific characters from a string in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)