Java.lang.String.regionMatches() Method

Advertisements


Description

The java.lang.String.regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) method tests if two string regions are equal.A substring of this String object is compared to a substring of the argument other.

The result is true if these substrings represent character sequences that are the same, ignoring case if and only if ignoreCase is true. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index ooffset and has length len.

Declaration

Following is the declaration for java.lang.String.regionMatches() method

public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

Parameters

  • 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.

Return Value

This method returns true if the specified subregion of this string matches the specified subregion of the string argument, else false.

Exception

  • NA

Example

The following example shows the usage of java.lang.String.regionMatches() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    String str1 = "Collection of tutorials";
    String str2 = "Consists of different tutorials";

    /* matches characters from index 14 in str1 to characters from
    index 22 in str2 considering same case of the letters */
    boolean match1 = str1.regionMatches(14, str2, 22, 9);
    System.out.println("region matched = " + match1);
    
    /* considering different case, "true" is set which will ignore
    case when matched */
    str2 = "Consists of different Tutorials";
    match1 = str1.regionMatches(true, 14, str2, 22, 9); 
    System.out.println("region matched = " + match1);   
  }
}

Let us compile and run the above program, this will produce the following result:

region matched = true
region matched = true



Advertisements
Advertisements