正则表达式:

判断一个字符串是不是都是数字,普通方法

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
public class Demo {
    public static void main(String[] args) {
        String str = "42a3432";
        char[] chars = str.toCharArray();
        boolean flag = true;
        int i = 0;
        for (; i < chars.length; i++) {
            if (chars[i]<'0'|chars[i]>'9'){
                flag = false;
                break;
            }
        }
        if (flag){
            System.out.println("全部都是数字");
        }
        else{
            System.out.println("第"+i+"个不是数字");
        }
    }
}

  

正则表示式

public class Demo {
    public static void main(String[] args) {
        String str = "423432";

        Pattern compile = Pattern.compile("\\d*");
        Matcher m = compile.matcher(str);
        System.out.println(m.matches());  //开始真正的匹配

        //字符串自带匹配正则
        boolean matches = str.matches("\\d*");
        System.out.println(matches);
    }
}

  

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄