Java regionMatches() Method

Java String类Java String Class


The regionMatches() method is used to test whether two strings are equal in a region.

Syntax

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

或

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

Parameters

  • ignoreCase-- If true, case is ignored 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

Returns true if the specified subregion of this string matches the specified subregion of the string argument; otherwise, returns false. Whether the match is exact or case-sensitive depends on the ignoreCase parameter.

Example

Example

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("www.example.com");
        String Str2 = new String("example");
        String Str3 = new String("EXAMPLE");

        System.out.print("Return value :" );
        System.out.println(Str1.regionMatches(4, Str2, 0, 5));

        System.out.print("Return value :" );
        System.out.println(Str1.regionMatches(4, Str3, 0, 5));

        System.out.print("Return value :" );
        System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));
    }
}
}

The above program produces the following result:

返回值 :true
返回值 :false
返回值 :true

Java String类Java String Class

Other Extensions