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

1. String’s 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

1.1 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);

1.2 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)

1.3 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

1.4 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)

2. Examples on regionMatches() method:

2.1 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

2.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.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String replace() method
Java - String matches() method