java.lang.String regionMatches()

Description :

This java tutorial shows how to use the regionMatches() method of String class. This method returns a boolean value true or false. This is generally tests if two string region are equal. To put it simple we are comparing two strings out of the substrings of those string sources.

Method Syntax :

Below are the two overloaded method of regionMatches().

Method 1:

public boolean regionMatches(int toffset, String other, int ooffset,int len)

Method 2:

public boolean regionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len)

Parameter input :


DataType Parameter Description
int offset this String subregion starting offset
String other the string parameter argument
int ooffset the parameter regions starting offset
int len this is the number of characters we intended to compare
boolean ignorecase this is a flag telling the method if we want to compare the region regardless of case


Method Returns :

This method returns a boolean datatype. true if the character region matches the specified region of the string parameter.

Exception :

No Exception is expected as specified on java api.

Java Code Example :

This example source code demonstrates the use of regionMatches() method of String class.

package com.javatutorialhq.tutorial;

 * This sample source code shows how to use the regionMatches method of String class

public class StringRegionMatchesDemo {

    public static void main(String[] args) {
        String source = "hello world";
        String anotherString = "World of java";
        System.out.println(source.regionMatches(6, anotherString, 0, 5));
        System.out.println(source.regionMatches(true,6, anotherString, 0, 5));


Sample Output :

Running the regionMatches() example source code will give you the following output

java string regionMatches method example

java string regionMatches method example

Exception Scenario :

No Exception on Java API

Suggested Reading List :