//isDigit
public static bool isNumberic1(this string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
foreach (char c in _string)
{
if (!char.IsDigit(c))//if(c<'0' || c>'9')//最好的方法,在下面测试数据中再加一个0,然后这种方法效率会搞10毫秒左右
return false;
}
return true;
}
//vb isnumberic
public static bool isNumberic2(this string _string)
{
return !string.IsNullOrEmpty(_string) && Microsoft.VisualBasic.Information.IsNumeric(_string);
}
//try parese
public static bool isNumberic3(this string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
int i = 0;
return int.TryParse(_string, out i);
}
//try catch
public static bool isNumberic4(this string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
try { int.Parse(_string); }
catch { return false; }
return true; }
//regex
public static bool isNumberic5(this string _string)
{
return !string.IsNullOrEmpty(_string) && Regex.IsMatch(_string, "^\d+$");
}