Open In App

Java String regionMatches() Method with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The regionMatches() method of the String class has two variants that can be used to test if two string regions are matching or equal. There are two variants of this method, i.e., one is case sensitive test method, and the other ignores the case-sensitive method.

Syntax:

1. Case sensitive test method:

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

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

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

Parameters:

  • ignoreCase: if true, ignore the case when comparing characters.
  • toffset: the starting offset of the subregion in this string.
  • other: the string argument being compared.
  • offset: the starting offset of the subregion in the string argument.
  • len: the number of characters to compare.

Return Value:

A substring of the String object is compared to a substring of the argument other. The result is true if these substrings represent character sequences that are the same, ignoring case if and only if ignoreCase is true. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index offset and has length len. The result is false if and only if at least one of the following is true

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

Note: The method returns false if at least one of these is true,

  • toffset is negative.
  • offset is negative.
  • toffset+len is greater than the length of this String object.
  • offset+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)))


Last Updated : 19 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads