Search

Check if all numeric string

This C# code snippet checks to see if the specified string is all numeric, that is, contains no alphabetic characters.

using System.Text.RegularExpressions;
...
const string ALL_NUMERIC_PATTERN = "[a-z|A-Z]";
 
static readonly Regex All_Numeric_Regex = 
   new Regex (ALL_NUMERIC_PATTERN);
 
static bool AllNumeric ( string inputString )
{
   if (All_Numeric_Regex.IsMatch ( inputString ))
   { 
      return false;
   }
   return true;
}
Below is another solution:
using System.Text.RegularExpressions;
...
static bool AllNumeric(string inputString)
{
   return Regex.IsMatch(inputString, @"^\d+$");
}