Menu
Topics Index
...
`

StringComparisonregionMatches Demo - Java Example Program


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);

© meritcampus 2019

All Rights Reserved.

Open In App