The regionMatches() method has two variants which can be used to test if two string regions are matching or equal.
Case sensitive test method:
public boolean regionMatches(int toffset, String other, int ooffset, int len)
It has the option to consider or ignore the case method:
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
- ignoreCase : if true, ignore case when comparing characters.
- toffset : the starting offset of the subregion in this string.
- other : the string argument being compared.
- ooffset : the starting offset of the subregion in the string argument.
- len : the number of characters to compare.
It returns true if the specified subregion of this string matches the specified subregion of the string argument, false otherwise. If ignoreCase is given true then it returns true or false being case insensitive.
Example 1 :
Result of Comparing of String 1 and String 2: true Result of Comparing of String 1 and String 3: false Result of Comparing of String 2 and String 3: false
Result of comparing String 1 and String 2 : true Result of comparing String 1 and String 3 : false Result of comparing String 2 and String 3 : true
The method returns false if atleast one of these are true,
- toffset is negative.
- ooffset is negative.
- toffset+len is greater than the length of this String object.
- ooffset+len is greater than the length of the other argument.
- ignoreCase is false and there is some nonnegative integer k less than len such that:
this.charAt(toffset+k) != other.charAt(ooffset+k)
- ignoreCase is true and there is some nonnegative integer k less than len such that:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java Program to Print all the Strings that Match a Given Pattern from a File
- Scanner match() method in Java with Example
- Printing Integer between Strings in Java
- Numbers in Java (With 0 Prefix and with Strings)
- Swap two Strings without using third user defined variable in Java
- Compare two strings lexicographically in Java
- Output of Java Programs | Set 52 (Strings Class)
- Check whether two Strings are Anagram of each other using HashMap in Java
- Compare two Strings in Java
- Java Program to Count number of binary strings without consecutive 1's
- Strings in Java
- Compact Strings in Java 9 with Examples
- Java Program to Write an Array of Strings to the Output Console
- How to Initialize and Compare Strings in Java?
- Print all possible strings of length k that can be formed from a set of n characters
- Find all palindromic sub-strings of a given string | Set 2
- TCS Coding Practice Question | Concatenate 2 Strings
- Check if two strings are same ignoring their cases
- Number of character corrections in the given strings to make them equal
- Sort an array of strings according to string lengths using Map
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.