- 用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("[0-9]*"); 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; }
感谢那些勤劳的人无私且详细的总结,让我们的职业生涯少了很多绊脚石,感恩感谢