w3resource

Java String: regionMatches() Method

public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

The regionMatches() method is used to test if two string regions are equal. A substring of this String object is compared to a substring of the argument other.

The result is true if these substrings represent character sequences that are the same, ignoring case if and only if ignoreCase is true. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index ooffset and has length len. The result is false if and only if at least one of the following is 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:
  • Character.toLowerCase(this.charAt(toffset+k)) !=
    Character.toLowerCase(other.charAt(ooffset+k))

    and:

    Character.toUpperCase(this.charAt(toffset+k)) !=
    Character.toUpperCase(other.charAt(ooffset+k))

Java Platform: Java SE 8

Syntax:

regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

Parameters:

Name Description Type
ignoreCase if true, ignore case when comparing characters. boolean
toffset the starting offset of the subregion in this string. int
other the string argument. String
ooffset the starting offset of the subregion in the string argument. int
len the number of characters to compare. int

Return Value:true if the specified subregion of this string matches the specified subregion of the string argument; false otherwise. Whether the matching is exact or case insensitive depends on the ignoreCase argument.

Return Value Type: char

Example:Java String regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) Method

The following example shows the usage of java String() method.

public class Example {

public static void main(String[] args)
    {
       //String str1 = "Red Green Orange Yellow";
        //String str2 = "Yellow Orange Green Red";

        String str1 = "Shanghai Houston Colorado Alexandria";
        String str2 = "Alexandria Colorado Houston Shanghai";

        // Determine whether characters 0 through 7 in str1 
        // match characters 28 through 35 in str2.
boolean match1 = str1.regionMatches(0, str2, 28, 8);

        // Determine whether characters 9 through 15 in str1 
        // match characters 9 through 15 in str2.
boolean match2 = str1.regionMatches(9, str2, 9, 8);
System.out.println();
        // Display the results of the regionMatches method calls.
System.out.println("str1[0 - 7] == str2[28 - 35]? " + match1);
System.out.println("str1[9 - 15] == str2[9 - 15]? " + match2);
System.out.println();
    }
}

Output:

 str1[0 - 7] == str2[28 - 35]? true                     
str1[9 - 15] == str2[9 - 15]? false 

Java Code Editor:

Previous:offsetByCodePoints Method
Next:replace Method



Follow us on Facebook and Twitter for latest update.