Java regionMatches example:
Sometimes we need to compare two substrings in different string in Java. We can do that by comparing each character one by one of both strings but Java String class comes with a built-in method called _regionMatches _to make this task easier.
This method has two different variants. In this tutorial, we will learn how to use _regionMatches _method to compare sub-strings of two different strings.
Syntax of regionMatches :
regionMatches has two variants. The first one is as below :
public boolean regionMatches(int toffset, String other, int ooffset, int len)
This method uses case sensitive comparison of two sub-strings.
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
This method comes with one extra parameter ignoreCase. We can use this param to change the comparison case sensitive or non-case sensitive. Following are the descriptions of the parameters used in the method :
ignoreCase: If true, it will ignore case while doing the comparison. If false, the comparison will be case-sensitive.
tooffset: The starting offset of the subregion in the current string.
other: Second string.
ooffset: The starting offset of the subregion in the second string.
len: Number of characters in the string to compare.
This method will return _true _if the substring in both strings matches. Otherwise, it will return false.
Example Program :
Let’s take a look at the example program to learn how it works :
public class Example {
public static void main(String[] args) {
//1
String str1 = "Hello World";
String str2 = "And hello Universe";
String str3 = "Hello Again";
//2
System.out.println("Region matching 1 : " + str1.regionMatches(0, str2, 4, 5));
//3
System.out.println("Region matching 2 : " + str1.regionMatches(0, str3, 0, 5));
//4
System.out.println("Region matching 3 : " + str1.regionMatches(true, 0, str2, 4, 5));
//5
System.out.println("Region matching 4 : " + str1.regionMatches(false, 0, str2, 4, 5));
}
}
Output :
Region matching 1 : false
Region matching 2 : true
Region matching 3 : true
Region matching 4 : false
Explanation :
The commented numbers in the above program denote the step numbers below :
-
Create three strings str1,str2 and str3 first. We will use these strings to test the regionMatches method.
-
The first print method printed false. Here we are comparing str1 with str2. Start position for str1 is 0 and the start position for str2 is 4. We are comparing 5 characters in both of these strings. i.e. we are comparing ’Hello’ from str1 with ’hello’ from str2. But since the first character H is different in case on both of these strings, it will return false.
-
In this print statement, we are comparing str1 and str3. The substring for these two strings is ’Hello’. It will return true since both are the same.
-
Similar to step 2 comparison, we are comparing str1 and str2 here. Comparison is with ’Hello’ and ’hello’ substrings. Both are different. But we are passing true as the ignoreCase parameter. So, it will print true.
-
The last print statement is similar to the previous one. The only difference is that we are passing false for ignoreCase. As we are considering the character case, this method will return false.
This program is also available on Github
Conclusion :
regionMatches is really a useful method for comparing two substrings in Java. You can use this method to quickly compare two substrings of different strings instead of writing a new method to do the same. Try to run the above examples and drop one comment below if you have any queries.