Java String regionMatches() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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
CallWhat it does
"Hello World".regionMatches(6, "World", 0, 5)true: chars 6-10 equal "World".
"Hello World".regionMatches(6, "world", 0, 5)false: W vs w.
"Hello World".regionMatches(true, 6, "world", 0, 5)true: case ignored.
"Java".regionMatches(2, "va!", 0, 3)false: the region would run past the end.

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

ParameterDescription
ignoreCaseOptional. true to ignore letter case.
toffsetStart of the region in this string.
otherThe string to compare with. Must not be null.
ooffsetStart of the region in other.
lenHow many chars to compare.

Return Value

Return ValueDescription
trueBoth regions exist and are equal (ignoring case if requested).
falseA 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.

java
public class RegionMatchesBasic {
    public static void main(String[] args) {
        String greeting = "Hello World";

        System.out.println(greeting.regionMatches(6, "World Cup", 0, 5));
        System.out.println(greeting.regionMatches(6, "world", 0, 5));
        System.out.println(greeting.regionMatches(true, 6, "world", 0, 5));
    }
}

How It Works

  1. Line up the regions. Index 6 of "Hello World" starts "World"; index 0 of "World Cup" starts the same five letters, so the first call is true.
  2. One capital spoils it. Against "world" the very first pair is 'W' vs 'w', so the case-sensitive call returns false.
  3. Flip the switch. Passing true as the first argument compares the letters case-insensitively and the regions now match.

Example 2 — Case-Insensitive File Extension Check

Test for .jpg at the end of a name whatever its case, without building a lowercase copy.

java
public class RegionMatchesExtension {
    public static void main(String[] args) {
        String[] files = {"PHOTO.JPG", "holiday.jpg", "notes.txt"};
        String ext = ".jpg";

        for (String f : files) {
            boolean isJpg = f.regionMatches(true, f.length() - ext.length(), ext, 0, ext.length());
            System.out.println(f + " -> " + isJpg);
        }
    }
}

How It Works

  1. Aim at the tail. f.length() - ext.length() is where the last four chars begin: 5 for "PHOTO.JPG".
  2. Case does not matter. ".JPG" and ".jpg" compare equal with ignoreCase = true, so both photos pass.
  3. No extra strings. "notes.txt" fails on 't' vs 'j'; unlike toLowerCase().endsWith(...), nothing new is allocated.

Example 3 — Regions That Do Not Fit Return false

Compare what happens with bad offsets in regionMatches and in substring.

java
public class RegionMatchesBounds {
    public static void main(String[] args) {
        String word = "Java";

        System.out.println(word.regionMatches(2, "va!", 0, 3));
        System.out.println(word.regionMatches(-1, "Ja", 0, 2));
        System.out.println(word.regionMatches(2, "va", 0, 2));

        try {
            word.substring(2, 5);
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("substring threw " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. 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.
  2. Negative offset. -1 is not a valid start, and again the method returns false rather than throwing.
  3. 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.

Next: repeat()

Repeat a string n times (Java 11+).

repeat() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful