学无先后,达者为师

网站首页 编程语言 正文

String类的matches()方法的使用

作者:小黑孩666 更新时间: 2022-07-22 编程语言

java.lang包中的String类,java.util.regex包中的Pattern,Matcher类中都有matches()方法。

该方法大多用于正则表达式

例子:给String类型的ageStr赋值为字符串型数据,判断ageStr是否包含正整数(  [1-9]\\d*  为正则表达式,大家可找一个正则表达式的在线生成器生成一个)

public class test {
    public static void main(String[] args) {
        String ageStr="dsadasdsa";
        //包含正整数
        boolean b =ageStr.matches("[1-9]\\d*");
        System.out.println("b = " + b);
    }
}

返回结果为false

如果把ageStr的值改为数值型,例如:

public class test {
    public static void main(String[] args) {
        String ageStr="1213";
        //包含正整数
        boolean b =ageStr.matches("[1-9]\\d*");
        System.out.println("b = " + b);
    }
}

则执行结果为  true

 

原文链接:https://blog.csdn.net/xiaoheihai666/article/details/124856799

栏目分类
最近更新