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

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;
}
原文地址:https://www.cnblogs.com/zl1991/p/6254280.html