In this article, we will discuss regionMatches() method of String class which is used to check whether two substrings are same or NOT
1. String’s regionMatches() method:
- This String method is used to check whether sub-string of invoking-string exactly matches sub-string of the specified string
- Note: there are 2 variants or overloaded regionMatches() method
1.1 Method Signature:
public boolean regionMatches(int toffset, String other,
int ooffset, int len);
public boolean regionMatches(boolean ignoreCase, int toffset,
String other, int ooffset, int len);
1.2 Parameters:
- toffset –> starting index-position for invoking string (or substring)
- other –> another string (or substring) to which invoking string need to be compared
- ooffset –> starting index-position for another string (or substring)
- len –> number of characters to be compared (from another string)
- ignoreCase –> if true, ignore case differences when comparing characters (applicable for 2nd variant)
1.3 Note:
- There are 2 strings
- One is invoking string i.e.; original string
- And another string
- Sub-string from both original string & another string will be compared
1.4 Returns:
- Returns true, if the sub-string of invoking-string exactly matches sub-string of the specified string (i.e.; both sub-string are same)
- Returns false, if the sub-string of invoking-string doesn’t matches sub-string of the specified string (i.e.; both sub-string aren’t same)
2. Examples on regionMatches() method:
2.1 Example checks for exact matches between different strings
- considers case-differences i.e.; case-sensitive
- whereas, 2nd variant allows to specify whether we want to ignore or consider case-differences
StringRegionMatchesMethod.java
package in.bench.resources.string.methods;
public class StringRegionMatchesMethod {
public static void main(String[] args) {
// sample string values
String originalStr = "This is Java World";
String anotherStr = "Java";
String anotherStr2 = "world"; // case-difference
String anotherStr3 = "Another world is JEE"; // middle-str
// 1st check for string 'Java'
System.out.println(
"originalStr(8, 4) == anotherStr(0, 4) : "
+ originalStr.regionMatches(8, anotherStr, 0, 4));
// 2nd check for string 'world' - case difference
System.out.println("\n"
+ "originalStr(13, 5) == anotherStr2(0, 5) : "
+ originalStr.regionMatches(13, anotherStr2, 0, 5));
// 3rd check for string 'is' - in middle of string
System.out.println("\n"
+ "originalStr(5, 2) == anotherStr3(14, 2) : "
+ originalStr.regionMatches(5, anotherStr3, 14, 2));
}
}
Output:
originalStr(8, 4) == anotherStr(0, 4) : true
originalStr(13, 5) == anotherStr2(0, 5) : false
originalStr(5, 2) == anotherStr3(14, 2) : true
2.2 This example very similar to above example but ignores case differences i.e.; case-insensitive
- if 1st parameter is kept false, then considers case-differences
- otherwise if kept true, ignores case-differences (very similar to 1st variant)
Note:
- for considering case-differences, use 1st variant
- use 2nd variant only when we want to ignore case-differences (by setting 1st parameter as false)
StringRegionMatchesMethodForCase.java
package in.bench.resources.string.methods;
public class StringRegionMatchesMethodForCase {
public static void main(String[] args) {
// sample string values
String originalStr = "This is Java World";
String anotherStr = "Java";
String anotherStr2 = "world"; // case-difference
String anotherStr3 = "Another world IS J2ee";//middle-str
// 1st check for string 'Java'
System.out.println(""
+ "originalStr(8, 4) == anotherStr(0, 4) "
+ "considering case-difference : "
+ originalStr.regionMatches(
false, 8, anotherStr, 0, 4));
// 2nd check for string 'world'
// considering case-difference
System.out.println("\n"
+ "originalStr(13, 5) == anotherStr2(0, 5) "
+ "considering case-difference : "
+ originalStr.regionMatches(
false, 13, anotherStr2, 0, 5));
// 3rd check for string 'world'
// ignoring case-differences
System.out.println("\n"
+ "originalStr(13, 5) == anotherStr2(0, 5) "
+ "ignoring case-difference : "
+ originalStr.regionMatches(
true, 13, anotherStr2, 0, 5));
// 3rd check for string 'is' - in middle of string
System.out.println("\n"
+ "originalStr(5, 2) == anotherStr3(14, 2) "
+ "considering case-difference : "
+ originalStr.regionMatches(
false, 5, anotherStr3, 14, 2));
}
}
Output:
originalStr(8, 4) == anotherStr(0, 4)
considering case-difference : true
originalStr(13, 5) == anotherStr2(0, 5)
considering case-difference : false
originalStr(13, 5) == anotherStr2(0, 5)
ignoring case-difference : true
originalStr(5, 2) == anotherStr3(14, 2)
considering case-difference : false
Hope, you found this article very helpful. If you any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
- https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum
Happy Coding !!
Happy Learning !!