C Method to verify that a given string is a number

  • 2021-01-19 22:23:06
  • OfStack

This article demonstrates C#'s method of verifying whether a given string is a number. Share with you for your reference. The specific analysis is as follows:

The C # code is used to verify if the given string is a number, cannot be used to verify a negative number, a string can only appear in Numbers and the decimal point, otherwise don't think Numbers, not the length of the verification number, that is to say, a given string longer, even if it is 10000 characters, can also be verified, so by verifying the string not 1 can be turned into C # int type and Int64 type.


/// <summary>
///  Verify that it is a number 
/// </summary>
/// <param name="number"> Number to verify </param>    
public static bool IsNumber(string number)
{
  // If it is empty, the validation is considered failed 
  if (IsNullOrEmpty(number))
  {
 return false;
  }
  // Clear the whitespace in the string to validate 
  number = number.Trim();
  // Pattern string 
  string pattern = @"^[0-9]+[0-9]*[.]?[0-9]*$";
  // validation 
  return RegexHelper.IsMatch(number, pattern);
}

I hope this article is helpful to your C# program design.


Related articles: