java 判断string是否为数字的方法

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");

 

posted @ 2022-11-10 15:13  酷盖的小机灵  阅读(466)  评论(0编辑  收藏  举报