Java中用正则表达式判断字符串是否全是数字

编程入门 行业动态 更新时间:2024-10-18 14:25:38

Java<a href=https://www.elefans.com/category/jswz/34/1771335.html style=中用正则表达式判断字符串是否全是数字"/>

Java中用正则表达式判断字符串是否全是数字

    public boolean isNumeric(String str) {//Pattern pattern = Patternpile("^-?[0-9]+"); //这个也行Pattern pattern = Patternpile("^-?\\d+(\\.\\d+)?$");//这个也行Matcher isNum = pattern.matcher(str);if (!isNum.matches()) {return false;}return true;}

以上两种正则表达式均正确

        System.out.println(new Test().isNumeric("sd21"));//falseSystem.out.println(new Test().isNumeric("2113 23"));//falseSystem.out.println(new Test().isNumeric(""));//falseSystem.out.println(new Test().isNumeric("12#12"));//falseSystem.out.println(new Test().isNumeric("-11212"));//trueSystem.out.println(new Test().isNumeric("11212"));//trueSystem.out.println(new Test().isNumeric("11212SDS"));//false

更多推荐

Java中用正则表达式判断字符串是否全是数字

本文发布于:2024-02-06 09:42:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1748293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中用   字符串   数字   正则表达式   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!