1、用JAVA自带的函数
1 public static boolean isNumeric(String str) { 2 for (int i = 0; i < str.length(); i++) { 3 System.out.println(str.charAt(i)); 4 if (!Character.isDigit(str.charAt(i))) { 5 return false; 6 } 7 } 8 return true; 9 }
2、用正则表达式
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
1 public boolean isNumeric(String str) { 2 Pattern pattern = Pattern.compile("[0-9]*"); 3 Matcher isNum = pattern.matcher(str); 4 if (!isNum.matches()) { 5 return false; 6 } 7 return true; 8 }
3、使用org.apache.commons.lang
1 boolean isNumericFlag = StringUtils.isNumeric("111");
标签:return,string,Pattern,boolean,str,isNumeric,java,数字 From: https://www.cnblogs.com/yxl-wyb/p/16877147.html