In this section, you will get the detailed explanation about the regionMatches() method of String class. We are going to use regionMatches() method of String class in Java.
String regionMatches()
In this section, you will get the detailed explanation about the
regionMatches() method of
String class. We are going to use regionMatches() method of String class in Java. The description of the code is given below for the usage of the method.
Description of the code:
As shown in the example a substring of the specified string object is compared to a substring of the other. If these substrings contain same characters sequence then we get true as output i.e. if the specified subregion of the string matches the subregion of the string argument; otherwise we get false. The substring to be compared begins at index toffset and has length len and that of other substring to be compared begins at index ooffset and has length len.
Therefore, in the program code given below the characters from 20 through 28 in str1 match characters 25 through 33 in str2 and give true as output.
However if one of the following is true then only we get false as an output:
- if toffset is negative.
- If ooffset is negative.
- If toffset + len is greater than the length of the String object.
- If ooffset + len is greater than the length of the other argument.
Parameters:
toffset - the starting offset of the subregion in this string.
ooffset - the starting offset of the subregion in the string argument.
len - the number of characters to compare.
NOTE: This method tests if two string regions are equal or not.
Here is the code of the program:
|
Output of the program:
C:\unique>javac matchstr.java C:\unique>java matchstr true C:\unique> |