Identify if a string is numeric in C#
This post will discuss how to identify if a given string is numeric or not in C#.
There are several methods to check if the given string is numeric in C#:
1. Using Regular Expression
The idea is to use the regular expression ^[0-9]+$ or ^\d+$, which checks the string for numeric characters. This can be implemented using the Regex.IsMatch() method, which tells whether the string matches the given regular expression. To allow empty strings, just replace + with *.
Note that regex [0-9] is not equivalent to \d. [0-9] matches with a character in the range 0 through 9, while \d matches ASCII 0-9 and other digit characters like Eastern Arabic numerals ٠١٢٣٤٥٦٧٨٩.
|
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 s = "123"; if (Regex.IsMatch(s, @"^\d+$")) { Console.WriteLine("Given string is numeric"); } else { Console.WriteLine("Given string is non-numeric"); } } } /* Output: Given string is numeric */ |
If the regular expression is frequently called, compile the regular expression first for faster execution in subsequent calls.
|
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(@"^\d+$"); public static void Main() { string s = "123"; if (regex.IsMatch(s)) { Console.WriteLine("Given string is numeric"); } else { Console.WriteLine("Given string is non-numeric"); } } } /* Output: Given string is numeric */ |
2. Using Enumerable.All() method
LINQ’s Enumerable.All() method returns true when all elements of a sequence satisfy a condition. To test for numeric characters, pass Char.IsDigit to the Enumerable.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 s = "123"; if (s.All(char.IsDigit)) { Console.WriteLine("Given string is numeric"); } else { Console.WriteLine("Given string is non-numeric"); } } } |
Note that the IsDigit() method does not strictly check for a character in the range 0 through 9. It allows a few characters such as Thai numerals ๐ ๑ ๒ ๓ ๔ ๕ ๖ ๗ ๘ ๙. We can use the following code to strictly check for ASCII digits:
|
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.Linq; public class Example { public static void Main() { string s = "123"; if (s.All(c => (c >= 48 && c <= 57))) { Console.WriteLine("Given string is numeric"); } else { Console.WriteLine("Given string is non-numeric"); } } } /* Output: Given string is numeric */ |
3. Using Enumerable.Any() method
LINQ’s Enumerable.Any() method returns true when any element of a sequence satisfy a condition. To test for numeric characters, escape the range 0 to 9. The following code example shows how to implement this.
|
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.Linq; public class Example { public static void Main() { string s = "123"; if (!s.Any(c => c < '0' || c > '9')) { Console.WriteLine("Given string is numeric"); } else { Console.WriteLine("Given string is non-numeric"); } } } /* Output: Given string is numeric */ |
4. Using Int.TryParse() method
To determine whether a string is numeric, convert it into its 32-bit signed integer equivalent using the Int32.TryParse() method. Following is a simple example demonstrating usage of this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; public class Example { public static bool isNumeric(string s) { return int.TryParse(s, out int n); } public static void Main() { string s = "123"; if (isNumeric(s)) { Console.WriteLine("Given string is numeric"); } else { Console.WriteLine("Given string is non-numeric"); } } } /* Output: Given string is numeric */ |
5. Using foreach loop
A naive solution is to iterate over characters of the string and check each character to be numeric. 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 |
using System; public class Example { public static bool isNumeric(string s) { foreach (char c in s) { if (!(c >= '0' && c <= '9')) { return false; } } return true; } public static void Main() { string s = "123"; if (isNumeric(s)) { Console.WriteLine("Given string is numeric"); } else { Console.WriteLine("Given string is non-numeric"); } } } /* Output: Given string is numeric */ |
That’s all about checking if a string is numeric 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 :)