Open In App

Java String regionMatches() Method with Examples

Last Updated : 19 Dec, 2022
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)))


Similar Reads

StringJoiner Class vs String.join() Method to Join String in Java with Examples
Prior to Java 8 when we need to concatenate a group of strings we need to write that code manually in addition to this we needed to repeatedly use delimiter and sometimes it leads to several mistakes but after Java 8 we can concatenate the strings using StringJoiner class and String.join() method then we can easily achieve our goal. Example: Withou
6 min read
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. Variants of String match
4 min read
Java String equalsIgnoreCase() Method with Examples
In Java, equalsIgnoreCase() method of the String class compares two strings irrespective of the case (lower or upper) of the string. This method returns a boolean value, true if the argument is not null and represents an equivalent String ignoring case, else false. Syntax of equalsIgnoreCase()str2.equalsIgnoreCase(str1);Parametersstr1: A string tha
2 min read
Java String format() Method With Examples
In Java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Syntax of String format() There are two types of string format() methods mentioned below: public static String
3 min read
Java String subSequence() method with Examples
The Java.lang.String.subSequence() is a built-in function in Java that returns a CharSequence. CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at (end-1). The length (in chars) of the returned sequence is (end-start, so if start == end then an empty
2 min read
String Class repeat() Method in Java with Examples
The string can be repeated N number of times, and we can generate a new string that has repetitions. repeat() method is used to return String whose value is the concatenation of given String repeated count times. If the string is empty or the count is zero then the empty string is returned. Syntax: string.repeat(count); Parameter: Accepts an intege
2 min read
String toString() Method in java with Examples
String toString() is the built-in method of java.lang which return itself a string. So here no actual conversion is performed. Since toString() method simply returns the current string without any changes, there is no need to call the string explicitly, it is usually called implicitly. Syntax : public String toString() Parameter: The method does no
1 min read
Matcher quoteReplacement(String) method in Java with Examples
The quoteReplacement(String string) method of Matcher Class is used to get the replacement String literal of the String passed as parameter. This String literal acts as the parameter for the replace methods. Hence quoteReplacement() method acts as the intermediate in the replace methods. Syntax: public static String quoteReplacement(String string)
2 min read
Matcher start(String) method in Java with Examples
The start(String string) method of Matcher Class is used to get the start index of the match result already done, from the specified string. Syntax: public int start(String string) Parameters: This method takes a parameter string which is the String from which the start index of the matched pattern is required. Return Value: This method returns the
2 min read
Matcher group(String) method in Java with Examples
The group(String string) method of Matcher Class is used to get the group of the match result already done, from the specified string. Syntax: public String group(String string) Parameters: This method takes a parameter string which is the String from which the group index of the matched pattern is required. Return Value: This method returns the gr
2 min read