Menu
Topics Index
...
`


Strings > String Comparison >
Siva Nookala - 20 Feb 2017
The regionMatches method compares a specific region in a String with a specific region in another String. It returns trueif both the regions in both the strings are having same sequence of characters, else it returns false.

The regionMatches has two forms. The general form is
boolean regionMatches(int startIndex, String str, int strStartIndex, int numChars)
Here startIndex specifies starting index of region of the invoked string. str is the String to be compared. strStartIndex is the starting index of the region of the String str. numChars is the length of the substring being compared.
The second form of regionMatches is the overloaded form. It includes option for ignoring the case while comparing.
boolean regionMatches(boolean ignoreCase, int startIndex, String str, int strStartIndex, int numChars)
Here if ignoreCase is true, then the comparison is not case-sensitive else if ignoreCase is false the comparison is case-sensitive.
StringComparisonregionMatches Demo
class RegionCompare
{
    public static void main(String arg[])
    {
        String fruit = "apple";
        boolean result = fruit.regionMatches(0, "pineapple", 4, 5);
        System.out.println("apple regionMatches pineapple -- " + result);
        boolean result1 = fruit.regionMatches(0, "pineapple", 0, 4);
        System.out.println("apple regionMatches pineapple -- " + result1);
        boolean result2 = fruit.regionMatches(0, "pineAPPLE", 4, 5);
        System.out.println("apple regionMatches pineAPPLE -- " + result2);
        boolean result3 = fruit.regionMatches(false, 0, "pineAPPLE", 4, 5);
        System.out.println("apple regionMatches pineAPPLE -- " + result3);
        boolean result4 = fruit.regionMatches(true, 0, "pineAPPLE", 4, 5);
        System.out.println("apple regionMatches pineAPPLE -- " + result4);
    
    }
}
OUTPUT

apple regionMatches pineapple -- true
apple regionMatches pineapple -- false
apple regionMatches pineAPPLE -- false
apple regionMatches pineAPPLE -- false
apple regionMatches pineAPPLE -- true

DESCRIPTION

The first output line is true since the String apple matches with the region apple in the String pineapple. The second output line is false since the region appl in the String apple does not match with the region pine in the String pineapple.The third output line is false since the region apple in the String apple does not match with the region APPLE in the String pineAPPLE.

The fourth output line is false since we mentioned ignoreCase as false, the region apple in the String apple does not match with the region APPLE in the String pineAPPLE. The fifth output line is true since we mentioned ignorecase as true, the region apple in the String apple matches with the region APPLE in the String pineAPPLE even if both the regions of the strings are of different case.

THINGS TO TRY
  • Place the below shown code at the end of the main method and observe the output.
    boolean result5 = "hello".regionMatches(3, "lol", 0, 3);
    System.out.println("hello regionMatches lol -- " + result5);
  • Place the below shown code at the end of the main method and observe the output.
    boolean result6 = "hello".regionMatches(3, "lol", 0, 2);
    System.out.println("hello regionMatches lol -- " + result6);

Dependent Topics : Java equals method vs == Operator  Java Searching Strings - Java indexOf, lastIndexOf Methods  Java String substring() method - substring In Java 

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.