A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 徐梦侠 中级黑马   /  2012-10-16 12:13  /  1375 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

找到四种方法,第一种最常用,第二种最先学会用,第三种最方便。
1.用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
  if (!Character.isDigit(str.charAt(i))){
   return false;
  }
}
return true;
}
2.用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;
}
3.正则表达式
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
4.还是正则表达式
public static boolean isNumeric(String str){
if(str.matches("\\d*"){
return true;
}else{
  return false;
}
}

评分

参与人数 1黑马币 +15 收起 理由
韩军博 + 15 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马