Java-RegionMatches

RegionMatches

The String class provides one more specialized method for comparison. It is regionMatches( ) method. This method enables us to compare a specific region inside a string with another specific region in another string.

For example, in the following program, a substring of length ‘4’ starting from the position ‘3’ in the String object str1 is formed. Than, another substring of length ‘4’ starting from the position ‘0’ in the string str2 is formed. These two substrings are then compared.

Program


1

Program Source

public class Javaapp {
    
    public static void main(String[] args){
        
        String str1 = new String("123456789");
        String str2 = new String("4567");
        
        if(str1.regionMatches(3, str2, 0, 4))
        {
            System.out.println("str2 values found inside str1");
        }
   }
}

Leave a Comment