當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。