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