Remove non-alphanumeric characters from a string in C#
This post will discuss how to remove non-alphanumeric characters from a string in C#.
If we’re to remove all non-alphanumeric characters from a string, we have to create a new string instance since strings are immutable in C#. This post provides an overview of several methods to accomplish this:
1. Using Regular Expression
The idea is to check for non-alphanumeric characters in a string and replace them with an empty string. We can use the regular expression [^a-zA-Z0-9] to identify non-alphanumeric characters in a string. Replace the regular expression [^a-zA-Z0-9] with [^a-zA-Z0-9 _] to allow spaces and underscore character.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string str = "25-Dec@2019!!"; str = Regex.Replace(str, "[^a-zA-Z0-9]", String.Empty); Console.WriteLine(str); } } /* Output: 25Dec2019 */ |
If the regular expression is frequently called, you might want to pre-compile the regular expression for a faster search on the cost of increased startup time.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.Text.RegularExpressions; public class Example { private static readonly Regex regex = new Regex("[^a-zA-Z0-9]"); public static void Main() { string str = "25-Dec@2019!!"; str = regex.Replace(str, String.Empty); Console.WriteLine(str); } } /* Output: 25Dec2019 */ |
In ASCII, word characters are [a-zA-Z0-9_]. The regular expression \w and \W check for the word and non-word characters, respectively. Therefore, we can use regular expression [^\w]* or [\W]* to identify non-alphanumeric characters in a string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string str = "25-Dec@2019!!"; str = Regex.Replace(str, @"[^\w]*", String.Empty); Console.WriteLine(str); } } /* Output: 25Dec2019 */ |
2. Using Array.FindAll() method
The Array.FindAll() method returns all elements of the specified sequence which satisfies a certain condition. To filter only alphanumeric characters, pass Char.IsLetterOrDigit to the FindAll() method, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; public class Example { public static void Main() { string str = "25-Dec@2019!!"; str = String.Concat(Array.FindAll(str.ToCharArray(), Char.IsLetterOrDigit)); Console.WriteLine(str); } } /* Output: 25Dec2019 */ |
Note that the IsLetterOrDigit() method does not strictly check for ASCII characters in range A-Z, a-z, and 0-9. We can write our custom logic to check characters in the desired range, 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 |
using System; public class Example { public static bool isAlphaNumeric(char c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); } public static void Main() { string str = "25-Dec@2019!!"; str = String.Concat(Array.FindAll(str.ToCharArray(), isAlphaNumeric)); Console.WriteLine(str); } } /* Output: 25Dec2019 */ |
3. Using LINQ
Another similar solution uses LINQ’s Where() method to filter elements of a sequence based on a condition. We can use this as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Linq; public class Example { public static void Main() { string str = "25-Dec@2019!!"; str = String.Concat(str.Where(char.IsLetterOrDigit)); Console.WriteLine(str); } } /* Output: 25Dec2019 */ |
That’s all about removing all non-alphanumeric 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 :)