this.治疗完毕

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

//1.单个字符逐一检查

public 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; 
  }

//2.正则表达的写法是: 
public bool IsNumeric(string str)  
{   
   System.Text.RegularExpressions.Regex reg1  
       = new System.Text.RegularExpressions.Regex(@"^[-]?/d+[.]?/d*$");   
   return reg1.IsMatch(str);  
}

 

posted on 2018-05-31 19:05  this.治疗完毕  阅读(110)  评论(0)    收藏  举报