Check if a string consists of alphanumeric characters in C#
This post will discuss how to check if a string consists of alphanumeric characters or not in C#.
There are several methods to determine whether the given string is alphanumeric (consists of only numbers and alphabets) in C#:
1. Using Regular Expression
The idea is to use the regular expression ^[a-zA-Z0-9]*$, which checks the string for alphanumeric characters. This can be done using the Regex.IsMatch() method, which tells whether this string matches the given regular expression. To restrict empty strings, use + instead of *.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string str = "ABC123"; if (Regex.IsMatch(str, "^[a-zA-Z0-9]*$")) { Console.WriteLine("Alphanumeric String"); } else { Console.WriteLine("Non-Alphanumeric String"); } } } /* Output: Alphanumeric String */ |
If the regular expression is frequently called, you might want to compile the regular expression for the performance boost. This results in faster execution but increases startup time.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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 = "ABC123"; if (regex.IsMatch(str)) { Console.WriteLine("Alphanumeric String"); } else { Console.WriteLine("Non-Alphanumeric String"); } } } /* Output: Alphanumeric String */ |
2. Using Enumerable.All() method
LINQ’s All() method returns true when all elements of a sequence satisfy a condition. To test for alphanumeric characters, pass the IsLetterOrDigit to the All() method.
|
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 = "ABC123"; if (str.All(char.IsLetterOrDigit)) { Console.WriteLine("Alphanumeric String"); } else { Console.WriteLine("Non-Alphanumeric String"); } } } |
Note that the IsLetterOrDigit() method does not strictly check for characters in ASCII range A-Z, a-z, and 0-9. We can use the following code to strictly check for ASCII alphabets and numbers:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Linq; public class Example { public static void Main() { string str = "ABC123"; if (str.All(c => (c >= 48 && c <= 57 || c >= 65 && c <= 90 || c >= 97 && c <= 122))) { Console.WriteLine("Alphanumeric String"); } else { Console.WriteLine("Non-Alphanumeric String"); } } } /* Output: Alphanumeric String */ |
3. Naive Solution
A naive solution is to iterate over characters in the string and check each character to be alphanumeric. 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 31 32 33 34 |
using System; public class Example { public static bool isAlphaNumeric(string s) { foreach (char c in s) { if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z') && !(c >= '0' && c <= '9')) { return false; } } return true; } public static void Main() { string str = "ABC123"; if (isAlphaNumeric(str)) { Console.WriteLine("Alphanumeric String"); } else { Console.WriteLine("Non-Alphanumeric String"); } } } /* Output: Alphanumeric String */ |
That’s all about determining whether a string consists of alphanumeric characters 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 :)