C#验证输入的是否数字的方法

来源:互联网 发布:access数据库设计 视频 编辑:程序博客网 时间:2024/06/03 07:20

static bool IsNumeric(string str)
  {
   if (str==null || str.Length==0)
    return false;
   foreach(char c in str)
   {
    if (!Char.IsNumber(c))
    {
     return false;
    }
   }
   return true;
  }

正则表达的写法是:

static bool IsNumeric(string str) 
{  
   System.Text.RegularExpressions.Regex reg1 
       = new System.Text.RegularExpressions.Regex(@"^[-]?/d+[.]?/d*$");  
  
return reg1.IsMatch(str); 
}