STRING INSTANCE METHODS
Java String regionMatches() method
regionMatches() compares selected regions of two String values. We specify the starting index in each string and the length to compare. The method returns a boolean and lets us choose whether to ignore case.
Comparing two string regions
String tekstas = "Hello, welcome to Java!";
String dalis = "welcome";
boolean sutampa = tekstas.regionMatches(7, dalis, 0, 7);
System.out.println(sutampa);true
tekstasstores the sentence, whiledalisstores the word welcome.- Indices start at 0. In the first string, w is at index 7.
- In the second string, we start at index 0 and compare 7 characters.
- Both regions contain welcome, so the result is
true. Text before and after the selected region does not affect the comparison.
Why is the method called on a String object?
regionMatches() is an instance method: the first string is the receiver, and the second string is passed as the argument corresponding to the parameter other.
boolean sutampa = tekstas.regionMatches(7, dalis, 0, 7);
We do not write String.regionMatches(...), because the method needs a specific String instance. Both original strings remain unchanged.
Method syntax
public boolean regionMatches(int toffset, String other, int ooffset, int len)
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
| Part | Meaning |
|---|---|
toffset | Starting index in the receiver. |
other | The other String to compare. |
ooffset | Starting index in the second string. |
len | Length to compare in UTF-16 char units, rather than an end index. |
ignoreCase | true ignores case; false compares case. |
boolean | true means the selected regions match; false means they differ or extend beyond the string bounds. |
The overload without ignoreCase is case-sensitive. It produces the same result as the overload with ignoreCase = false.
Case-sensitive comparison
String tekstas = "Welcome to JavaQuest!";
String vardas = "JavaQuest";
String didziosios = "JAVAQUEST";
System.out.println(tekstas.regionMatches(11, vardas, 0, 9));
System.out.println(tekstas.regionMatches(11, didziosios, 0, 9));
System.out.println(vardas.regionMatches(0, didziosios, 0, 9));true
false
false
JavaQuest starts at index 11 and has 9 characters. The first comparison matches. In the other comparisons, case differs, so the result is false.
Case-insensitive comparison
String tekstas = "Java";
String mazosios = "java";
String didziosios = "JAVA";
System.out.println(tekstas.regionMatches(true, 0, mazosios, 0, 4));
System.out.println(tekstas.regionMatches(false, 0, didziosios, 0, 4));
System.out.println(mazosios.regionMatches(true, 0, didziosios, 0, 4));true
false
true
The first boolean argument controls case sensitivity only. true allows Java, java, and JAVA to match, while false requires matching uppercase and lowercase letters.
Different starting indices
String pirmas = "ID: JAVA-2026";
String antras = "Code: JAVA-9999";
System.out.println(pirmas.regionMatches(4, antras, 6, 4));
System.out.println(pirmas.regionMatches(4, antras, 6, 9));true
false
In the first string, JAVA starts at index 4; in the second, it starts at index 6. Comparing 4 characters matches JAVA. Comparing 9 also includes the numbers: JAVA-2026 differs from JAVA-9999. len is a count, so each region runs from its starting index up to start + len, excluding that final boundary.
Practical example: checking a command
String komanda = "START mission";
String prefiksas = "start ";
boolean pradeti = komanda.regionMatches(true, 0, prefiksas, 0, prefiksas.length());
if (pradeti) {
System.out.println(true);
} else {
System.out.println(false);
}true
We check whether the command begins with start followed by a space, ignoring case. prefiksas.length() supplies the comparison length. The space distinguishes the command from text such as STARTER. This example does not validate the rest of the command.
Out-of-bounds regions return false
String tekstas = "Java";
System.out.println(tekstas.regionMatches(-1, "Java", 0, 4));
System.out.println(tekstas.regionMatches(0, "Java", -1, 4));
System.out.println(tekstas.regionMatches(1, "Java", 0, 4));
System.out.println(tekstas.regionMatches(0, "Ja", 0, 4));false
false
false
false
A negative starting index or insufficient characters in either string produces false. regionMatches() does not throw IndexOutOfBoundsException in these cases. In the third call, only 3 characters remain after index 1; in the fourth, the other string has only 2 characters.
Zero length and empty strings
String tekstas = "Java";
System.out.println(tekstas.regionMatches(4, "", 0, 0));
System.out.println("".regionMatches(0, "", 0, 0));
System.out.println(tekstas.regionMatches(5, "", 0, 0));
System.out.println("".regionMatches(0, "J", 0, 1));true
true
false
false
When len is 0 and both starting indices are within 0 through the respective string length(), the empty regions match. However, index 5 is beyond Java even with zero length. An empty string cannot supply one character. Use a nonnegative len; if the length comes from input, validate len >= 0.
Differences between regionMatches(), equals(), contains(), and matches()
String tekstas = "Learn Java";
System.out.println(tekstas.equals("Java"));
System.out.println(tekstas.contains("Java"));
System.out.println(tekstas.regionMatches(0, "Java", 0, 4));
System.out.println(tekstas.regionMatches(6, "Java", 0, 4));
System.out.println(tekstas.matches(".*Java"));false
true
false
true
true
equals() compares whole strings, contains() searches for a literal sequence anywhere, and matches() checks the whole string against a regex. regionMatches() compares only the specified regions: it neither searches other positions nor interprets regex symbols.
UTF-16 indices and language rules
String tekstas = "A😀Java";
System.out.println(tekstas.length());
System.out.println(tekstas.regionMatches(3, "Java", 0, 4));
System.out.println(tekstas.regionMatches(2, "Java", 0, 4));7
true
false
The emoji occupies two UTF-16 char units, so Java starts at index 3 rather than 2. Starting indices and len are measured in char units, rather than the number of visible characters.
ignoreCase = true does not perform locale-sensitive comparison. Use java.text.Collator when you need comparison rules for a particular language.
The other string cannot be null
String tekstas = "Java";
String kitas = null;
System.out.println(tekstas.regionMatches(0, kitas, 0, 4));NullPointerException
When other is null, there is no second string region to compare, so this call throws NullPointerException. Calling the method on a null reference also fails. This differs from equals(null), which returns false.
Common mistakes
lenis confused with an end index.- Swapping the starting indices of the two strings.
- Expecting
regionMatches()to find a match elsewhere in the string. - Forgetting that the overload without a
booleanargument is case-sensitive. - Expecting a bounds exception even though the method returns
false. - Counting an emoji as one index position or passing
null.
When should you use regionMatches()?
Use regionMatches() when you know the starting positions and length of the regions: for command prefixes, parts of codes, or fragments of structured input. You can compare regions directly without calling substring(). For whole-string equality, use equals() or equalsIgnoreCase(); to search an unknown position, use contains() or indexOf().