Java String API regionMatches()
Quick guide to Java String API regionMatches() Method with Examples. This method is used to compare two sub strings. Syntax: public boolean regionMatches(int toffset, String other, int ooffset, int len)
1. Java String regionMatches() Overview
In this tutorial, We’ll learn about Java String API regionMatches() method to compare two substrings. In other words, comparing regions of two strings.
This method is very handy when we want to compare portions of two strings. Instead of comparing all contents of Strings.
In previous article, We’ve discussed on String matches() method.
1.1 regionMatches() Syntax
1 2 | public boolean regionMatches( int toffset, String other, int ooffset, int len) public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) |
Fist variant does case sensitive comparison
Second variant has option to ignore the case. If true, it ignores case when comparing.
1.2 Parameters
toffset – the starting offset of the subregion in this string.
other – the string argument.
ooffset – the starting offset of the subregion in the string argument.
len – the number of characters to compare.
1.3 Returns
1 | boolean |
true if the specified subregion of this string exactly matches the specified
subregion of the string argument; false otherwise.
2. String regionMatches() Examples
We’ll write a example programs using regionMatches() method. It is very important to pass the parameters in order and with required values.
2.1 regionMatches() Method Example
Below example program is written on regionMatches method. For this method, we must have to pass the parameters as below.
1st parameter: Start index of the current string. Comparison will start from this point.
2nd parameter: Takes another string which is to be compared.
3rd parameter: Start index of the another string.
4th parameter: How many characters to be compared.
1 2 3 4 5 6 7 8 9 | String str1 = "welcome to java-w3schools blog" ; String otherStr = "java" ; boolean isMatch = str1.regionMatches(11, otherStr, 0, 4); if (isMatch) { System.out.println( "Substrings are matched" ); } else { System.out.println( "Substrings are not matched" ); } |
Output:
1 | Substrings are matched |
2.2 Case Ignore regionMatches() Method Example
The below program does compare substrings by ignoring Case Type. We need to pass the additional parameter to enable case ignore.
01 02 03 04 05 06 07 08 09 10 | // Example 2: Case Ignore String str2 = "WELCOME TO JAVA-W3SCHOOLS BLOG" ; String otherStr2 = "java" ; isMatch = str2.regionMatches( true , 11, otherStr2, 0, 4); if (isMatch) { System.out.println( "Substrings are matched" ); } else { System.out.println( "Substrings are not matched" ); } |
Observe the above program. A boolean value is passed in 1st parameter. This parameter tells to JVM to ignore case.
Output:
1 | Substrings are matched |
3. String regionMatches() Internal Code
regionMatches() method internal implementation code shown below.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public boolean regionMatches( int toffset, String other, int ooffset, int len) { byte tv[] = value; byte ov[] = other.value; // Note: toffset, ooffset, or len might be near -1>>>1. if ((ooffset < 0) || (toffset < 0) || (toffset > ( long )length() - len) || (ooffset > ( long )other.length() - len)) { return false ; } if (coder() == other.coder()) { if (!isLatin1() && (len > 0)) { toffset = toffset << 1; ooffset = ooffset << 1; len = len << 1; } while (len-- > 0) { if (tv[toffset++] != ov[ooffset++]) { return false ; } } } else { if (coder() == LATIN1) { while (len-- > 0) { if (StringLatin1.getChar(tv, toffset++) != StringUTF16.getChar(ov, ooffset++)) { return false ; } } } else { while (len-- > 0) { if (StringUTF16.getChar(tv, toffset++) != StringLatin1.getChar(ov, ooffset++)) { return false ; } } } } return true ; } |
The result is false if and only if at least one of the following is true:
A)toffset is negative.
B)ooffset is negative.
C)toffset+len is greater than the length of this String object.
D)ooffset+len is greater than the length of the other argument.
E) There is some nonnegative integer k less than
len such that: this.charAt(toffset + k) != other.charAt(ooffset + k)
Later it compares char by char. If char mismatch it returns true, otherwise returns false.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 | public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) { if (!ignoreCase) { return regionMatches(toffset, other, ooffset, len); } // Note: toffset, ooffset, or len might be near -1>>>1. if ((ooffset < 0) || (toffset < 0) || (toffset > ( long )length() - len) || (ooffset > ( long )other.length() - len)) { return false ; } byte tv[] = value; byte ov[] = other.value; if (coder() == other.coder()) { return isLatin1() ? StringLatin1.regionMatchesCI(tv, toffset, ov, ooffset, len) : StringUTF16.regionMatchesCI(tv, toffset, ov, ooffset, len); } return isLatin1() ? StringLatin1.regionMatchesCI_UTF16(tv, toffset, ov, ooffset, len) : StringUTF16.regionMatchesCI_Latin1(tv, toffset, ov, ooffset, len); } |
If ignoreCase is false then it calls regionMatches(toffset, other, ooffset, len). If it is true then calls StringLatin1.regionMatchesCI() for LATIN Character Set.
It calls StringUTF16.regionMatchesCI() for UTF Character Set.
4. Conclusion
In this article, We’ve seen what regionMatches() method does. And
how to compare small portions of two strings.
Further in this article, discussed example program on how to ignore case while comparing.
Shown regionMatches() inernal implementation and how it works internally.
Example code snippets shown in this article is available on GitHub.
Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Java String API regionMatches() Opinions expressed by Java Code Geeks contributors are their own. |