Easy Tutorial
❮ Data Vec Max Java Stringtokenizer Example ❯

Java Example - Testing if Two String Regions Are Equal

Java Examples

The following example uses the regionMatches() method to test if two string regions are equal:

StringRegionMatch.java File

public class StringRegionMatch {
   public static void main(String[] args) {
      String first_str = "Welcome to Microsoft";
      String second_str = "I work with microsoft";
      boolean match1 = first_str.
      regionMatches(11, second_str, 12, 9);
      boolean match2 = first_str.
      regionMatches(true, 11, second_str, 12, 9); // The first parameter true indicates case insensitivity
      System.out.println("Case-sensitive return value: " + match1);
      System.out.println("Case-insensitive return value: " + match2);
   }
}

firststr.regionMatches(11, secondstr, 12, 9) indicates that the first_str string starting from the 11th character "M" is compared with the second_str string starting from the 12th character "M", comparing 9 pairs of characters. Since the comparison is case-sensitive, the result is false.

If the first parameter is set to true, it indicates that the case difference is ignored, so the result is true.

The output of the above code example is:

Case-sensitive return value: false
Case-insensitive return value: true

Java Examples

❮ Data Vec Max Java Stringtokenizer Example ❯