Java - String regionMatches() Method



The Java String regionMatches() method checks whether the two strings regions are equivalent or not. It compares a substring of the current object to a substring of the given String.

This method returns true if these substrings represent identical character sequences. This method has two polymorphic variants. Their syntaxes is shown below where one is the case sensitive and the other ignores the case.

Syntax

Following is the syntax for Java String regionMatches() method −

public boolean regionMatches(int toffset, String other, int ooffset, int len) // first syntax
or,
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) // second syntax

Parameters

  • toffset − The starting offset of the subregion in this string.

  • other − The string argument.

  • ooffset − The starting offset of the subregion in the string argument.

  • len − The number of characters to compare.

  • ignoreCase − If true, ignore case, when comparing characters. # second syntax

Return Value

This method returns true if the specified subregion of this string exactly matches the specified subregion of the string argument, else false.

Example

The following example shows the usage of Java String regionMatches() method. Here we are initializing two String objects str1 and str2 and matching the characters from index “14” in the string “str1” to characters from index “22” in the string “str2” considering same case of the letters −

import java.lang.*; 
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "Collection of tutorials";
      String str2 = "Consists of different tutorials";
      /* matches characters from index 14 in str1 to characters from
         index 22 in str2 considering same case of the letters.*/
      boolean match1 = str1.regionMatches(14, str2, 22, 9);
      System.out.println("region matched = " + match1);    
      
      // considering different case, will return false
      str2 = "Consists of different Tutorials";
      match1 = str1.regionMatches(14, str2, 22, 9); 
      System.out.println("region matched = " + match1);   
   }
}

Output

If you compile and run the above program, it will produce the following result −

region matched = true
region matched = false

Example

By initializing the String objects and matching the characters from index "14" in the string "str1" to characters from index "22" in the string "str2" while taking into consideration the same case of the letters, the following example shows how to use the Java String regionMatches() method −

import java.lang.*; 
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "Collection of tutorials";
      String str2 = "Consists of different tutorials";
      /* matches characters from index 14 in str1 to characters from
         index 22 in str2 considering same case of the letters */
      boolean match1 = str1.regionMatches(14, str2, 22, 9);
      System.out.println("region matched = " + match1);    
      /* considering different case, "true" is set which will ignore
         case when matched */
      str2 = "Consists of different Tutorials";
      match1 = str1.regionMatches(true, 14, str2, 22, 9); 
      System.out.println("region matched = " + match1);   
   }
}

Output

If you compile and run the program above, the output will be displayed as follows −

region matched = true
region matched = true

Example

Below is another code of regionMatches() method where a string “Welcome to Tutorialspoint” is taken and it’s substring is considered as “TUTORIALS” and “Tutorials”. I index of “s1” is taken greater than the string length and the index of “s2” is taken within the string length −

import java.lang.*; 
public class StringDemo {
   public static void main(String args[]) {
      String s1 = new String("Welcome to Tutorialspoint");
      String s2 = new String("TUTORIALS");
      String s3 = new String("Tutorials");
      System.out.print("Return Value :");
      System.out.println(s1.regionMatches(50, s2, 0, 9)); 
      System.out.print("Return Value :");
      System.out.println(s1.regionMatches(11, s3, 0, 9));
   }
}

Output

On executing the program above, the output is obtained as follows −

Return Value :false
Return Value :true

Example

In the following example regionMatches() method is used to find whether the substrings or region of the given two strings are equal or not while passing the negative index −

import java.lang.*; 
public class StringDemo {
   public static void main(String args[]) {
      String s1 = new String("Welcome to TutorixProject");
      String s2 = new String("TUTORIX");
      String s3 = new String("Tutorix");
      System.out.print("Return Value :");
      System.out.println(s1.regionMatches(-11, s2, 0, 9));
      System.out.print("Return Value :");
      System.out.println(s1.regionMatches(-11, s3, 0, 9));
   }
}

Output

The output of the above program is as follows −

Return Value :false
Return Value :false
java_lang_string.htm
Advertisements