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 @   酷盖的小机灵  阅读(474)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示