C Determines whether the string is int and double (instance)

  • 2021-11-24 02:40:48
  • OfStack

Not much to say, please look at the code


using System.Text.RegularExpressions;
/// <summary>
///  Determine whether the string is int/double
/// </summary>
public static bool IsIntOrDouble(string strNumber)
{
 Regex objNotNumberPattern = new Regex("[^0-9.-]");
 Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
 Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
 const string strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
 const string strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
 Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
 return !objNotNumberPattern.IsMatch(strNumber) &&
  !objTwoDotPattern.IsMatch(strNumber) &&
  !objTwoMinusPattern.IsMatch(strNumber) &&
  objNumberPattern.IsMatch(strNumber);
}

Related articles: