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

 第一种利用java自带的函数

public static boolean isNumeric(String str){
          for(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();
}

猜你喜欢

转载自lihongtai.iteye.com/blog/2222737