1 /// <summary> 2 /// 判断字符串是否是数字 3 /// </summary> 4 /// <param name="str"></param> 5 /// <param name="nt"></param> 6 /// <returns></returns> 7 /// <summary> 8 public static bool IsNumType(this string str, NumType nt) 9 { 10 string matchString = ""; 11 switch (nt) 12 { 13 case NumType.NonNegativeInt: 14 //非负整数(正整数 + 0) 15 matchString = @"^[1-9]+[0-9]*$|^0$"; 16 break; 17 case NumType.PositiveInt: 18 //正整数 19 matchString = @"^[1-9]+[0-9]*$"; 20 break; 21 case NumType.NonPositiveInt: 22 //非正整数(负整数 + 0) 23 matchString = @"^-[1-9]+[0-9]*$|^0$"; 24 break; 25 case NumType.NegativeInt: 26 //负整数 27 matchString = @"^^-[1-9]+[0-9]*$"; 28 break; 29 case NumType.Int: 30 //整数 31 matchString = @"^-?[1-9]+[0-9]*$|^0$"; 32 break; 33 case NumType.NonNegative: 34 //非负数(正数 + 0) 35 matchString = @"(^(0\.0*[1-9]+[0-9]*$|[1-9]+[0-9]*\.[0-9]*[0-9]$|[1-9]+[0-9]*$)|^0$)"; 36 break; 37 case NumType.Positive: 38 //正数 39 matchString = @"^(0\.0*[1-9]+[0-9]*$|[1-9]+[0-9]*\.[0-9]*[0-9]$|[1-9]+[0-9]*$)"; 40 break; 41 case NumType.NonPositive: 42 //非正数(负数 + 0) 43 matchString = @"(^-(0\.0*[1-9]+[0-9]*$|[1-9]+[0-9]*\.[0-9]*[0-9]$|[1-9]+[0-9]*$)|^0$)"; 44 break; 45 case NumType.Negative: 46 //负数 47 matchString = @"^-(0\.0*[1-9]+[0-9]*$|[1-9]+[0-9]*\.[0-9]*[0-9]$|[1-9]+[0-9]*$)"; 48 break; 49 default: 50 break; 51 } 52 return Regex.IsMatch(str, matchString, RegexOptions.IgnoreCase); 53 }
时间: 2024-10-10 21:09:49