In this section, we will learn what the String regionMatches() method is and how to use it in Java.
What is Java String regionMatches() Method?
The `regionMatches()` method is used to compare a region of the target String with a region of a given string (as the argument to the method) and see if they match.
Java regionMatches() Method Syntax:
The method is overloaded and here are the syntaxes of the methods:
boolean regionMatches(int toffset, String other, int ooffset, int len) boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
regionMatches() Method Parameters:
- `toffset`: this value declares the starting index of the first string.
- `other`: this is the second string object that we want to compare part of its value with the first string.
- `ooffset`: this value declares the starting index of the second string.
- `len`: this number declares the number of characters from the starting index in each string object that we want to compare.
- `ignoreCase`: this is a boolean value, and it defines whether the case sensitivity should be on or off.
Note: by default, the value is `true` but we can set the value to `false` which means the method should not be case sensitive.
regionMatches() Method Return Value:
The return value of this method is of type Boolean.
If they matched, the result will be `true` otherwise `false`.
Example: using String regionMatches() method
public class Simple { public static void main(String args[]) { String str = "Welcome to future"; System.out.println(str.regionMatches(11,"future",0,6)); } }
Output: true