Java: String regionMatches() method

In this article, we will discuss regionMatches() method of String class which is used to check whether two substrings are same or NOT

 

regionMatches() method:

  • This String method is used to check whether sub-string of invoking-string exactly matches sub-string of the specified string
  • Note: there are 2 variants or overloaded regionMatches() method

Method Signature:

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

Parameters:

  • toffset            –> starting index-position for invoking string (or substring)
  • other             –> another string (or substring) to which invoking string need to be compared
  • ooffset           –> starting index-position for another string (or substring)
  • len                  –> number of characters to be compared (from another string)
  • ignoreCase   –> if true, ignore case differences when comparing characters (applicable for 2nd variant)

Note:

  • There are 2 strings
  • One is invoking string i.e.; original string
  • And another string
  • Sub-string from both original string & another string will be compared

Returns:

  • Returns true, if the sub-string of invoking-string exactly matches sub-string of the specified string (i.e.; both sub-string are same)
  • Returns false, if the sub-string of invoking-string doesn’t matches sub-string of the specified string (i.e.; both sub-string aren’t same)

 

Examples on regionMatches() method:

Example 1: below example checks for exact matches between different strings

  • considers case-differences i.e.; case-sensitive
  • whereas, 2nd variant allows to specify whether we want to ignore or consider case-differences

StringRegionMatchesMethod.java

package in.bench.resources.string.methods;

public class StringRegionMatchesMethod {

	public static void main(String[] args) {

		// sample string values
		String originalStr = "This is Java World";
		String anotherStr = "Java";
		String anotherStr2 = "world"; // case-difference
		String anotherStr3 = "Another world is JEE"; // middle-str

		// 1st check for string 'Java'
		System.out.println(
			"originalStr(8, 4) ==  anotherStr(0, 4) : "
			+ originalStr.regionMatches(8, anotherStr, 0, 4));

		// 2nd check for string 'world' - case difference
		System.out.println("\n"
			+ "originalStr(13, 5) ==  anotherStr2(0, 5) : "
			+ originalStr.regionMatches(13, anotherStr2, 0, 5));

		// 3rd check for string 'is' - in middle of string
		System.out.println("\n"
			+ "originalStr(5, 2) ==  anotherStr3(14, 2) : "
			+ originalStr.regionMatches(5, anotherStr3, 14, 2));
	}
}

Output:

originalStr(8, 4) ==  anotherStr(0, 4) : true

originalStr(13, 5) ==  anotherStr2(0, 5) : false

originalStr(5, 2) ==  anotherStr3(14, 2) : true

 

Example 2: this example very similar to above example but ignores case differences i.e.; case-insensitive

  • if 1st parameter is kept false, then considers case-differences
  • otherwise if kept true, ignores case-differences (very similar to 1st variant)

Note:

  • for considering case-differences, use 1st variant
  • use 2nd variant only when we want to ignore case-differences (by setting 1st parameter as false)

StringRegionMatchesMethodForCase.java

package in.bench.resources.string.methods;

public class StringRegionMatchesMethodForCase {

	public static void main(String[] args) {

		// sample string values
		String originalStr = "This is Java World";
		String anotherStr = "Java";
		String anotherStr2 = "world"; // case-difference
		String anotherStr3 = "Another world IS J2ee";//middle-str

		// 1st check for string 'Java'
		System.out.println(""
				+ "originalStr(8, 4) ==  anotherStr(0, 4) "
				+ "considering case-difference   : "
				+ originalStr.regionMatches(
						false, 8, anotherStr, 0, 4));

		// 2nd check for string 'world'
		// considering case-difference
		System.out.println("\n"
				+ "originalStr(13, 5) ==  anotherStr2(0, 5) "
				+ "considering case-difference : "
				+ originalStr.regionMatches(
						false, 13, anotherStr2, 0, 5));

		// 3rd check for string 'world'
		// ignoring case-differences
		System.out.println("\n"
				+ "originalStr(13, 5) ==  anotherStr2(0, 5) "
				+ "ignoring case-difference    : "
				+ originalStr.regionMatches(
						true, 13, anotherStr2, 0, 5));

		// 3rd check for string 'is' - in middle of string
		System.out.println("\n"
				+ "originalStr(5, 2) ==  anotherStr3(14, 2) "
				+ "considering case-difference : "
				+ originalStr.regionMatches(
						false, 5, anotherStr3, 14, 2));
	}
}

Output:

originalStr(8, 4) ==  anotherStr(0, 4) 
considering case-difference   : true

originalStr(13, 5) ==  anotherStr2(0, 5) 
considering case-difference : false

originalStr(13, 5) ==  anotherStr2(0, 5) 
ignoring case-difference    : true

originalStr(5, 2) ==  anotherStr3(14, 2) 
considering case-difference : false

 

Hope, you found this article very helpful. If you any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

 

Happy Coding !!
Happy Learning !!

Java: String replace() method
Java: String matches() method