Last Updated:

regionMatches method in Java | Examples

Today we'll look at an example of using a method of the String class in Java. We've already discussed the commonly used substring() and split() methods of the String class, but now let's move on to other interesting but rarely used ones.regionMatches

The regionMatches method in Java. Theory

The method is used to compare the substring of one row with the substring of another row, that is, it compares parts of rows and returns the result of this comparison - (if parts of the rows matched) or (did not match).regionMatchestruefalse

The Java String class implements 2 overloaded regionMatches methods:

Case-sensitive method:

and the case-ignore method:

They accept the same parameters, except for the first Boolean variable, which is responsible for the register.

Signature of the regionMatches method

  • ignoreCase is a Boolean variable with a value - ignore case, with a value - do not ignore the case.truefalse
  • toffset is the position from which we will start counting.
  • other is another line, part of which we will compare.
  • ooffset — the position from which we will start counting in the line other
  • len is the number of characters to compare

See below, an example will be easier.

Example of using the regionMatches method in Java

 

Let's look at a simple example of comparing parts of strings:

Let's take a look at what's going on here:

We have 2 lines, in one of which the word is regionMatches with a large M, and in the other with a small m.

In the first case, we say: Java, count 6 characters from the beginning of the first line and 18 characters from the beginning of the second line, and now compare the next 13 characters from the characters just specified in these lines.

After running this code, we will get:

In the second case, true, because we wanted to compare parts of the strings without being case sensitive.

Stay tuned to the Java for beginners section.