六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 31|回复: 0

java 判断是否为数字的三种方法

[复制链接]

升级  22.67%

22

主题

22

主题

22

主题

秀才

Rank: 2

积分
84
 楼主| 发表于 2013-2-7 15:41:40 | 显示全部楼层 |阅读模式
转自http://www.blogjava.net/hardson/archive/2006/10/11/58476.html
注:没有考虑负数、小数

//用JAVA自带的函数
public static boolean isNumeric(String str){
  for (int i = str.length();--i>=0;){   
   if (!Character.isDigit(str.charAt(i))){
    return false;
   }
  }
  return true;
}

//用正则表达式   
public static boolean isNumeric(String str){   
    Pattern pattern = Pattern.compile("[0-9]*");   
    return pattern.matcher(str).matches();      
}   

//用ascii码   
public static boolean isNumeric(String str){   
   for(int i=str.length();--i>=0;){   
      int chr=str.charAt(i);   
      if(chr<48 || chr>57)   
         return false;   
   }   
   return true;   
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表