当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.lang.String.matches()用法及代码示例


matches()方法有三种变体。本文介绍了所有这些内容,如下所示:1.字符串matches():此方法告诉此字符串是否与给定的正则表达式匹配。以str.matches(regex)形式调用此方法所产生的结果与表达式Pattern.matches(regex,str)完全相同。

用法:
public boolean matches(String regex)
参数
regex: the regular expression to which this string is to be matched.
Return Value
This method returns true if, and only if, this string matches the given regular expression.
// Java code to demonstrate the 
// working of matches()  
public class Match1 { 
      
   public static void main(String args[]) { 
        
      // Initializing String  
      String Str = new String("Welcome to geeksforgeeks"); 
        
      // Testing if regex is present 
      System.out.print("Does String contains regex (.*)geeks(.*) ?:" ); 
      System.out.println(Str.matches("(.*)geeks(.*)")); 
        
      // Testing if regex is present 
      System.out.print("Does String contains regex geeks ?:" ); 
      System.out.println(Str.matches("geeks")); 
        
   } 
}

输出:


Does String contains regex (.*)geeks(.*) ?:true
Does String contains regex geeks ?:false

2.字符串regionMatches()(带有ignoreCase):此方法有两个变体,可用于测试两个字符串区域是否相等。

Syntax
public boolean regionMatches(boolean ignoreCase,
                             int str_strt,
                             String other,
                             int other_strt,
                             int len)
参数
str_strt:the starting offset of the subregion in this string.
other:the string argument.
other_strt: the starting offset of the subregion in the string argument.
len: the number of characters to compare.
ignoreCase: if true, ignore case when comparing characters.
Return Value
It returns true if the specified subregion of this string
matches the specified subregion of the string argument;
false otherwise.
Whether the matching is exact or case insensitive depends on the ignoreCase argument.
// Java code to demonstrate the 
// working of regionmatches() 
public class Match2 { 
      
   public static void main(String args[]) { 
        
      // Initializing String  
      String Str1 = new String("Welcome to geeksforgeeks"); 
        
      // Initializing test String 
      String Str2 = new String("GEEKS"); 
        
      // Tests whether GEEKS starts in geeksforgeeks starting from pos 11 
      // and from 0 ( i.e starting in GEEKS) and ignores case 
      // and compares 5 characters of GEEKS 
      System.out.print("Checking if GEEKS is in geeksforgeeks( case insensitive ):" ); 
      System.out.println(Str1.regionMatches(true, 11, Str2, 0, 5)); 
        
   } 
}

输出:

Checking if GEEKS is in geeksforgeeks( case insensitive ):true

3.字符串regionMatches():此方法有两个变体,可用于测试两个字符串区域是否相等。

Syntax
public boolean regionMatches(int str_strt,
                             String other,
                             int other_strt,
                             int len)
参数
str_strt:the starting offset of the subregion in this string.
other:the string argument.
other_strt: the starting offset of the subregion in the string argument.
len: the number of characters to compare.
Return Value
It returns true if the specified subregion of this string
matches the specified subregion of the string argument;
false otherwise.
// Java code to demonstrate the 
// working of regionmatches() 
public class Match3 { 
      
   public static void main(String args[]) { 
        
      // Initializing String  
      String Str1 = new String("Welcome to geeksforgeeks"); 
        
      // Initializing test String 
      String Str2 = new String("GEEKS"); 
        
      // Tests whether GEEKS starts in geeksforgeeks starting from pos 11 
      // and compares 5 characters of GEEKS 
      System.out.print("Checking if GEEKS is in geeksforgeeks( case sensitive ):" ); 
      System.out.println(Str1.regionMatches(11, Str2, 0, 5)); 
        
   } 
}

输出:

Checking if GEEKS is in geeksforgeeks( case sensitive ):false


相关用法


注:本文由纯净天空筛选整理自 Java.lang.String.matches() in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。