一:
//1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < str.length(); i++){ System.out.println(str.charAt(i)); if (!Character.isDigit(str.charAt(i))){ return false; } } return true; }
//2.用正则表达式 首先要import java.util.regex.Pattern 和 java.util.regex.Matcher public boolean isNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if( !isNum.matches() ){ return false; } return true; }
原文:http://www.cnblogs.com/sunhaoyu/p/4936900.html