Java 判断字符串能否转化为数字的三种方法

  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. 用正则表达式
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();   
     } 
  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/yinyl/p/9673074.html