Java中 matches() 方法的作用是什么
更新:HHH   时间:2023-1-7


Java中 matches() 方法的作用是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

boolean matches(String regex):
matches() 方法用于检测字符串是否匹配给定的正则表达式。

调用此方法的 str.matches(regex) 形式与以下表达式产生的结果完全相同:

Pattern.matches(regex, str)

语法:

public boolean matches(String regex)

参数:
regex – 匹配字符串的正则表达式。

返回值:
在字符串匹配给定的正则表达式时,返回 true。

实例:

public class Demo1 {
    public static void main(String[] args){
        String Str = new String("我是一只小小鸟");

        System.out.println("返回值 :"+Str.matches("(.*)一只(.*)"));

        boolean flag = Str.matches("我是(.*)");
        if(flag){
            System.out.println("我被重新定义为:HelloWord!");
        }else{
            System.out.println("我没有被重新定义!");
        }
    }
}

以上程序执行结果为:

返回值 :true
我被重新定义为:HelloWord!

关于Java中 matches() 方法的作用是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注天达云行业资讯频道了解更多相关知识。

返回编程语言教程...