📜  Java中的字符串matches()方法及示例

📅  最后修改于: 2022-05-13 01:55:26.580000             🧑  作者: Mango

Java中的字符串matches()方法及示例

match() 方法的变体用于更准确地告诉不测试给定的字符串是否与正则表达式匹配,因为每当此方法本身被称为matches() 或者是matches() 时,我们确实传递了两个参数那是我们的字符串和正则表达式,工作和输出保持不变。

存在多个变体match() 方法三个变体 如下所列和描述如下:

变体 1:字符串匹配()

这个方法告诉我们这个字符串是否匹配给定的正则表达式。调用表单的这个方法 str.matches(regex)产生与表达式Pattern.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"));
    }
}


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));
    }
}


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));
        
   }
}


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

变体 2:字符串 regionMatches()

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

句法:

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:字符串 regionMatches() 与 ignoreCase

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

句法:

public boolean 
regionMatches(boolean ignoreCase, int str_strt, String other, int other_strt,int len)

参数:

  • 此字符串中子区域的起始偏移量
  • 字符串参数
  • 字符串参数中子区域的起始偏移量
  • 要比较的字符数
  • ignoreCase:如果为真,则在比较字符时忽略大小写

返回类型:如果此字符串的指定子区域与字符串参数的指定子区域匹配,则返回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