java中判断字符串是否为只包含数字

时间:2017-01-05 23:36:08   收藏:0   阅读:298

1.用Java自带的函数

1 public static boolean isNumeric(String str){
2   for(int i=str.length();--i>=0;){
3      if (!Character.isDigit(str.charAt(i))){
4         return false;
5     }
6   }
7   return true;
8 }

2.用正则表达式

1 public static boolean isNumeric(String str){
2   Pattern pattern =Pattern.compile("[0-9]*");
3   return pattern.matcher(str).matches();
4 }

3.用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;
}

 

原文:http://www.cnblogs.com/zl1991/p/6254280.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!