Skip to content
Related Articles
Open in App
Not now

Related Articles

String matches() Method in Java with Examples

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 12 Nov, 2021
Improve Article
Save Article

Variants of matches() method is used to tell more precisely not test whether the given string matches to a regular expression or not as whenever this method is called in itself as matches() or be it matches() where here we do pass two arguments that are our string and regular expression, the working and output remains same.

There exist multiple variants three variants of the matches() method as listed and described below as follows:  

Variant 1: String matches()

This method tells whether or not this string matches the given regular expression. An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str).

Syntax: 

public boolean matches(String regex) 

Parameters: The regular expression to which this string is to be matched. 

Return Type: Boolean value, returning true if and only if strings match the given regular expression else false.

Example:

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

Output

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

Variant 2: String regionMatches()

This method has two variants that can be used to test if two string regions are equal. 

Syntax:

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

Parameters:

  • The starting offset of the subregion in this string
  • The string argument
  • The starting offset of the subregion in the string argument
  • The number of characters to compare

Return Type: Boolean value, true if the specified subregion of this string matches the specified subregion of the string argument; false otherwise.

Example:

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

Output

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

Variant 3: String regionMatches() With ignoreCase

This method has two variants that can be used to test if two string regions are equal. 

Syntax:

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

Parameters:

  • The starting offset of the subregion in this string
  • The string argument
  • The starting offset of the subregion in the string argument
  • The number of characters to compare
  • ignoreCase:  if true, ignore the case when comparing characters

Return Type: 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.

Example:

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

Output: 

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

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!