⬅ Previous Topic
Java StringoffsetByCodePoints()
methodNext Topic ⮕
Java Stringreplace()
methodregionMatches()
method⬅ Previous Topic
Java StringoffsetByCodePoints()
methodNext Topic ⮕
Java Stringreplace()
methodThe regionMatches()
method in Java provides a way to efficiently check if a specific portion (a region) of one string matches another string or a region within it. It's particularly useful when you don't need to compare the entire strings, but only certain segments.
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
public boolean regionMatches(int toffset, String other, int ooffset, int len)
Parameter | Description |
---|---|
ignoreCase |
If true , case is ignored during comparison. Only applicable to the first overload. |
toffset |
The index in this string where the region starts. |
other |
The string to compare against. |
ooffset |
The index in other where the region starts. |
len |
The number of characters to compare. |
Returns true
if the specified regions of strings match; otherwise, returns false
.
This example demonstrates a case-sensitive comparison using the first overload. We'll compare a portion of one string with another, paying close attention to capitalization.
String str1 = "Hello World";
String str2 = "World";
bool result = str1.regionMatches(false, 6, str2, 0, 5);
System.out.println(result);
true
Explanation: We're comparing the substring of str1
starting at index 6 (which is "World") with str2
, which is also “World”, for a length of 5 characters. Since the case matches exactly, the method returns true
.
This example shows how to use the ignoreCase flag to perform a case-insensitive comparison. We'll compare portions of strings even if their capitalization differs.
String str1 = "Hello World";
String str2 = "world";
bool result = str1.regionMatches(true, 6, str2, 0, 5);
System.out.println(result);
true
Explanation: Here, the ignoreCase flag is set to true
. This means that capitalization is ignored during comparison. The substring of str1
starting at index 6 (“World”) is compared with str2
(“world”). Since case is ignored, they are considered a match, and the method returns true
.
This example demonstrates what happens when the regions being compared do not match.
String str1 = "Hello Java";
String str2 = "World";
bool result = str1.regionMatches(false, 6, str2, 0, 5);
System.out.println(result);
false
Explanation: We're comparing the substring of str1
starting at index 6 (which is "Java") with str2
, which is “World”. The substrings don’t match exactly; therefore, the method returns false
.
This example illustrates what happens when the specified length is too long and extends beyond either string's boundaries. The comparison stops at the end of the shorter sequence.
String str1 = "Hello World";
String str2 = "World";
bool result = str1.regionMatches(false, 6, str2, 0, 10); // len is longer than str2
System.out.println(result);
true
Explanation: We are trying to compare a length of 10 characters from str1 starting at index 6 with str2 which has only 5 characters. The comparison stops after the last character in str2. Because the initial portion matches, it returns true.
⬅ Previous Topic
Java StringoffsetByCodePoints()
methodNext Topic ⮕
Java Stringreplace()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.