sllizhimei 发表于 2013-2-7 15:41:40

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

转自http://www.blogjava.net/hardson/archive/2006/10/11/58476.html
注:没有考虑负数、小数

//用JAVA自带的函数
public static boolean isNumeric(String str){
for (int 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("*");   
    return pattern.matcher(str).matches();      
}   

//用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;   
}
页: [1]
查看完整版本: java 判断是否为数字的三种方法