Java String regionMatches example

Last updated on March 19th, 2017 at 12:24 pm

Previous
Next
String’s regionMatches is used to compare substring of one string to substring of another.Here region may be refer to substring of String, so we are comparing regions of one string with another.

Method Syntax: 

There are two overloaded version of regionMatches:

Parameters:

ignoreCase: It is boolean field to indicate whether to ignore case or not.
toffset : It is start index of sub String of String on which this method is being called.
other: It is other String by which this String will compare.
ooffset: It is start index of substring of other argument String
len : Number of characters to compare

 
Lets understand with the example:
 
You have two Strings and you want to match String java in both str1 and str2.

str1.regionMatches(17, str2, 5, 4) will do it. This expression will return true.
How?

17 is starting index of String java in str1.
str2 is our 2nd String
5 is starting index for String java in str2
4 is number of characters in String java

String regionMatches example :

When you run above program, you will get below output:

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog