String regionMatches() with ignoreCase

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

Syntax of String regionMatches()

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 of regionMatches() with 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 w3wiki");
 
        // Initializing a test string
        String Str2 = new String("GEEKS");
 
        // Tests whether GEEKS starts in w3wiki
        // 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 w3wiki( case insensitive ) : ");
 
        System.out.println(
            Str1.regionMatches(true, 11, Str2, 0, 5));
    }
}


Output

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


String matches() Method in Java with Examples

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.

Similar Reads

Variants of String matches() Method

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

1. String matches() Method

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)....

2. String regionMatches() Method

...

3. String regionMatches() with ignoreCase

...

Contact Us