In Java, regionMatches() compares part of this string with part of another string, character by character, and returns true when the two regions are equal. You choose where each region starts and how long it is.
The overload with a leading boolean ignoreCase compares letters without regard to case. That makes it the standard way to write a case-insensitive startsWith or endsWith without creating lowercase copies.
regionMatches() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.regionMatches(...); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.
Syntax
Say where the region starts in this string, which string to compare with, where its region starts, and how many chars to compare.
java
boolean same = text.regionMatches(6, other, 0, 5);
boolean any = text.regionMatches(true, 6, other, 0, 5); // ignore case
The comparison covers len chars: this[toffset ... toffset+len-1] against other[ooffset ... ooffset+len-1]. With ignoreCase, two chars are equal if they match after converting with Character.toUpperCase or Character.toLowerCase.
Library declarations
java
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
Parameter
Description
ignoreCase
Optional. true to ignore letter case.
toffset
Start of the region in this string.
other
The string to compare with. Must not be null.
ooffset
Start of the region in other.
len
How many chars to compare.
Return Value
Return Value
Description
true
Both regions exist and are equal (ignoring case if requested).
false
A char differs, an offset is negative, or a region would run past the end of its string.
Java Examples
Example 1 — Compare a Word Inside Two Strings
Check whether the second word of a greeting equals the first word of another string, with and without case.
Too long for this string. A 3-char region starting at index 2 would need indexes 2 to 4, but "Java" ends at 3, so the answer is simply false.
Negative offset.-1 is not a valid start, and again the method returns false rather than throwing.
The safe alternative. A region that fits ("va" at 2) matches, while substring(2, 5) on the same word throws StringIndexOutOfBoundsException.
Frequently Asked Questions
It compares a region of this string with a region of another string and returns true if they contain the same characters.
Use the overload with a boolean first argument: a.regionMatches(true, start, b, otherStart, length).
No. It returns false when an offset is negative or a region would run past the end. Only a null other string throws NullPointerException.
Call text.regionMatches(true, 0, prefix, 0, prefix.length()). It is true when text begins with prefix in any letter case.
toffset is where the region starts in this string, and ooffset is where it starts in the other string. len is how many characters both regions contain.