判断输入的是否是数字函數

来源:互联网 发布:linux如何删除ftp用户 编辑:程序博客网 时间:2024/05/09 08:14

判断输入的是否是数字函數
#region 判断输入的是否是数字函數
 /// <summary>
 /// 名称:IsNumber
 /// 功能:判断输入的是否是数字
 /// 参数:string strNumber:源文本
 /// 返回值: bool true:是 false:否
 
 public class myclass
 {
  /*
   * 判断字符串是否为数字函数,正则表达式
   */
  public bool IsNumber(String strNumber)
  {
   Regex objNotNumberPattern=new Regex("[^0-9.-]");
   Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
   Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
   String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
   String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
   Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
   return !objNotNumberPattern.IsMatch(strNumber) &&
    !objTwoDotPattern.IsMatch(strNumber) &&
    !objTwoMinusPattern.IsMatch(strNumber) &&
    objNumberPattern.IsMatch(strNumber);
  }   
 }
 #endregion

原创粉丝点击