Skip to content
Related Articles

Related Articles

Improve Article

How to Match Regions in Strings in Java?

  • Last Updated : 15 Oct, 2020

The regionMatches() method has two variants which can be used to test if two string regions are matching or equal.

Syntax:

Case sensitive test method:

public boolean regionMatches(int toffset, String other, int ooffset, int len) 

It has the option to consider or ignore the case method:

public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

Parameters:



  • ignoreCase : if true, ignore case when comparing characters.
  • toffset : the starting offset of the subregion in this string.
  • other : the string argument being compared.
  • ooffset : 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. If ignoreCase is given true then it returns true or false being case insensitive.

Example 1 :

Java




// Java Program to find if substrings or regions of two
// strings are equal
  
import java.io.*;
  
class CheckIfRegionsEqual {
    public static void main(String args[])
    {
  
        // create three string objects
        String str1 = new String(
                   "Welcome to Geeksforgeeks.com");
        String str2 = new String("Geeksforgeeks");
        String str3 = new String("GEEKSFORGEEKS");
  
        // Comparing str1 and str2
        System.out.print(
            "Result of Comparing of String 1 and String 2: ");
        System.out.println(
            str1.regionMatches(11, str2, 0, 13));
  
        // Comparing str1 and str3
        System.out.print(
            "Result of Comparing of String 1 and String 3: ");
        System.out.println(
            str1.regionMatches(11, str3, 0, 13));
  
        // Comparing str2 and str3
        System.out.print(
            "Result of Comparing of String 2 and String 3: ");
        System.out.println(
            str2.regionMatches(0, str3, 0, 13));
    }
}
Output
Result of Comparing of String 1 and String 2: true
Result of Comparing of String 1 and String 3: false
Result of Comparing of String 2 and String 3: false


Example 2:

Java




// Java Program to find if substrings or regions of two
// strings are equal
  
import java.io.*;
  
class CheckIfRegionsEqual {
    public static void main(String args[])
    {
  
        // create three string objects
        String str1 = new String("Abhishek Rout");
        String str2 = new String("abhishek");
        String str3 = new String("ABHISHEK");
  
        // Comparing str1 and str2 substrings
        System.out.print(
            "Result of comparing String 1 and String 2 : ");
        System.out.println(
            str1.regionMatches(true, 0, str2, 0, 8));
  
        // Comparing str1 and str3 substrings
        System.out.print(
            "Result of comparing String 1 and String 3 : ");
        System.out.println(
            str1.regionMatches(false, 0, str3, 0, 8));
  
        // Comparing str2 and str3 substrings
        System.out.print(
            "Result of comparing String 2 and String 3 : ");
        System.out.println(
            str2.regionMatches(true, 0, str3, 0, 8));
    }
}
Output
Result of comparing String 1 and String 2 : true
Result of comparing String 1 and String 3 : false
Result of comparing String 2 and String 3 : true


The method returns false if atleast one of these are true,

  • toffset is negative.
  • ooffset is negative.
  • toffset+len is greater than the length of this String object.
  • ooffset+len is greater than the length of the other argument.
  • ignoreCase is false and there is some nonnegative integer k less than len such that:

 this.charAt(toffset+k) != other.charAt(ooffset+k)
 

  • ignoreCase is true and there is some nonnegative integer k less than len such that:

 Character.toLowerCase(Character.toUpperCase(this.charAt(toffset+k))) !=
    Character.toLowerCase(Character.toUpperCase(other.charAt(ooffset+k)))

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :