首页 > 代码库 > C# 判断字符串是否是int/double

C# 判断字符串是否是int/double

 1 /// <summary>
 2 /// 判断字符串是否是int/double
 3 /// </summary>
 4 public static bool IsIntOrDouble(string strNumber)
 5 {
 6     Regex objNotNumberPattern = new Regex("[^0-9.-]");
 7     Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
 8     Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
 9     const string strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
10     const string strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
11     Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
12     return !objNotNumberPattern.IsMatch(strNumber) &&
13            !objTwoDotPattern.IsMatch(strNumber) &&
14            !objTwoMinusPattern.IsMatch(strNumber) &&
15            objNumberPattern.IsMatch(strNumber);
16 }

 

C# 判断字符串是否是int/double