Wednesday 8 February 2017

Java String regionMatches() Method



  This method compares a specific region inside a String with another specific region inside another String. The general form is:

boolean regionMatches( int startIndex,nString s2, int s2StartIndex, int numChars);

  Where startIndex is the starting position, s2 is the String to be compared, s2StartIndex is the starting position of in s2 from where the comparison will start and numChars is the length of String being compared to.
This method has another form as:

boolean regionMatches(boolean ignoreCase, int startIndex, String s2, int s2StartIndex, int numChars);

  Where ignoreCase is Boolean and if its value is true the method will ignore Case while comparing the Strings.
 Consider following example:

RegionMatchesTest.java


 public class RegionMatchesTest
 {
   public static void main(String args[])
   {
    String str1 = new String("Java is fun");
    String str2 = new String("Java");
    String str3 = new String("JAVA");

    System.out.print("Result of Test1: " );
    System.out.println(str1.regionMatches(0, str2, 0, 4));

    System.out.print("Result of Test2: " );
    System.out.println(str1.regionMatches(0, str3, 0, 4));

    System.out.print("Result of Test3: " );
   System.out.println(str1.regionMatches(true, 0, str3, 0, 4));
   }
 }

The output is:

Result of Test1: true
Result of Test2: false
Result of Test3: true



0 comments:

Post a Comment

Powered by Blogger.

Stats