The regionMatches() method in Java provides a way to efficiently check if a specific portion (a region) of one string matches another string or a region within it. It's particularly useful when you don't need to compare the entire strings, but only certain segments.
Java String regionMatches() method - Syntax and Examples
Syntax
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
public boolean regionMatches(int toffset, String other, int ooffset, int len)
Parameters
| Parameter | Description |
|---|---|
ignoreCase |
If true, case is ignored during comparison. Only applicable to the first overload. |
toffset |
The index in this string where the region starts. |
other |
The string to compare against. |
ooffset |
The index in other where the region starts. |
len |
The number of characters to compare. |
Return Value
Returns true if the specified regions of strings match; otherwise, returns false.
Examples
Example 1: Case-Sensitive Comparison
This example demonstrates a case-sensitive comparison using the first overload. We'll compare a portion of one string with another, paying close attention to capitalization.
String str1 = "Hello World";
String str2 = "World";
bool result = str1.regionMatches(false, 6, str2, 0, 5);
System.out.println(result);
true
Explanation: We're comparing the substring of str1 starting at index 6 (which is "World") with str2, which is also “World”, for a length of 5 characters. Since the case matches exactly, the method returns true.
Example 2: Case-Insensitive Comparison
This example shows how to use the ignoreCase flag to perform a case-insensitive comparison. We'll compare portions of strings even if their capitalization differs.
String str1 = "Hello World";
String str2 = "world";
bool result = str1.regionMatches(true, 6, str2, 0, 5);
System.out.println(result);
true
Explanation: Here, the ignoreCase flag is set to true. This means that capitalization is ignored during comparison. The substring of str1 starting at index 6 (“World”) is compared with str2 (“world”). Since case is ignored, they are considered a match, and the method returns true.
Example 3: Mismatch
This example demonstrates what happens when the regions being compared do not match.
String str1 = "Hello Java";
String str2 = "World";
bool result = str1.regionMatches(false, 6, str2, 0, 5);
System.out.println(result);
false
Explanation: We're comparing the substring of str1 starting at index 6 (which is "Java") with str2, which is “World”. The substrings don’t match exactly; therefore, the method returns false.
Example 4: Different Lengths
This example illustrates what happens when the specified length is too long and extends beyond either string's boundaries. The comparison stops at the end of the shorter sequence.
String str1 = "Hello World";
String str2 = "World";
bool result = str1.regionMatches(false, 6, str2, 0, 10); // len is longer than str2
System.out.println(result);
true
Explanation: We are trying to compare a length of 10 characters from str1 starting at index 6 with str2 which has only 5 characters. The comparison stops after the last character in str2. Because the initial portion matches, it returns true.
