Variants of matches()方法用于更精确地告诉您,不测试给定字符串是否与 正则表达式 或者,当这个方法本身被称为matches()或matches()时,我们在这里传递两个参数,即字符串和正则表达式,工作和输出保持不变。
存在多种变体 三 变种 matches()方法 如下所示:
变量1:字符串匹配()
这个方法告诉你这个字符串是否匹配给定的正则表达式。对这个表单方法的调用 str.matches(正则表达式) 产生与表达式完全相同的结果 图案匹配项(regex,str) .
语法:
public boolean matches(String regex)
参数: 此字符串要与之匹配的正则表达式。
返回类型: 布尔值,当且仅当字符串与给定正则表达式匹配时返回true,否则返回false。
例子:
JAVA
// Java Program to Demonstrate Working of matches() Method // of String class // Main class public class GFG { // Main driver method public static void main(String args[]) { // Declaring and initializing a string // Input string String Str = new String( "Welcome to geeksforgeeks" ); // Display message for better readability System.out.print( "Does String contains regex (.*)geeks(.*) ? : " ); // Testing if regex is present or not System.out.println(Str.matches( "(.*)geeks(.*)" )); // Display message for better readability System.out.print( "Does String contains regex geeks ? : " ); // Testing if regex is present or not System.out.println(Str.matches( "geeks" )); } } |
Does String contains regex (.*)geeks(.*) ? : true Does String contains regex geeks ? : false
变量2:字符串区域匹配()
该方法有两种变体,可用于测试两个字符串区域是否相等。
语法:
public boolean regionMatches(int str_strt, String other, int other_strt,int len)
参数:
- 此字符串中子区域的起始偏移量
- 字符串参数
- 字符串参数中子区域的起始偏移量
- 要比较的字符数
返回类型: 布尔值,如果此字符串的指定子区域与字符串参数的指定子区域匹配,则为true;否则就错了。
例子:
JAVA
// Java Program to Demonstrate Working of regionmatches() // method of String class // Main class public class GFG { // Main driver method public static void main(String args[]) { // Declaring and initializing a 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
变量3:带ignoreCase的字符串regionMatches()
该方法有两种变体,可用于测试两个字符串区域是否相等。
语法:
public boolean regionMatches(boolean ignoreCase, int str_strt, String other, int other_strt,int len)
参数:
- 此字符串中子区域的起始偏移量
- 字符串参数
- 字符串参数中子区域的起始偏移量
- 要比较的字符数
- 无知案例: 如果为true,则在比较字符时忽略大小写
返回类型: 如果此字符串的指定子区域与字符串参数的指定子区域匹配,则返回true;否则就错了。匹配是精确匹配还是不区分大小写取决于ignoreCase参数。
例子:
JAVA
// Java Program to Demonstrate Working of regionmatches() // Main class public class GFG { // Main driver method public static void main(String args[]) { // Declaring and initializing a string String Str1 = new String( "Welcome to geeksforgeeks" ); // Initializing a 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
本文由 阿斯塔·蒂亚吉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。