beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – String regionMatches() Method example

By Chaitanya Singh | Filed Under: String handling

The method regionMatches() tests if the two Strings are equal. Using this method we can compare the substring of input String with the substring of specified String.

Two variants:
public boolean regionMatches(int toffset, String other, int ooffset, int len): Case sensitive test.
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len): It has option to consider or ignore the case.

Parameters description:
ignoreCase– if true, ignore case when comparing characters.
toffset – the starting offset of the subregion in this string.
other – the string argument.
ooffset – the starting offset of the subregion in the string argument.
len – the number of characters to compare.

Example: regionMatches() method

public class RegionMatchesExample{
   public static void main(String args[]){
       String str1 = new String("Hello, How are you");
       String str2 = new String("How");
       String str3 = new String("HOW");

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

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

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

Output:

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

Reference:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#regionMatches(int, java.lang.String, int, int)

Enjoyed this post? Try these related posts

  1. Java String charAt() Method example
  2. Java String replace(), replaceFirst() and replaceAll() method
  3. Java String concat() Method example
  4. Java – String Class and its methods explained with examples
  5. Java String valueOf() method explained with examples
  6. Java String contains() method explained with examples

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorials

  • Learn Java
  • OOPs Concepts
  • Java Collections

Java String

  • Java String

Java String Methods

  • String charAt()
  • String compareTo()
  • String compareToIgnoreCase()
  • String contains()
  • String concat()
  • substring
  • String valueOf()
  • String startsWith()
  • String equals()
  • String format()
  • String endsWith()
  • String indexOf()
  • String lastIndexOf()
  • String length()
  • String replace()
  • String split()
  • String trim()
  • String intern()
  • String isEmpty()
  • String matches()
  • String regionMatches()
  • String contentEquals()
  • String toCharArray()
  • String getBytes()
  • String join()
  • String getChars()
  • String copyValueOf()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap