方法regionMatches()测试两个字符串是否相等。使用此方法,我们可以将输入String的子字符串与指定String的子字符串进行比较。
两种变体:
public boolean regionMatches(int toffset, String other, int ooffset, int len):区分大小写的测试。
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len):可以选择考虑或忽略大小写。
参数说明:
ignoreCase – 如果为true,则在比较字符时忽略大小写。
toffset – 此字符串中子区域的起始偏移量。
other – 字符串参数。
ooffset – 字符串参数中子区域的起始偏移量。
len – 要比较的字符数。
示例:regionMatches()方法
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)); }}输出:
Result of Test1: trueResult of Test2: falseResult of Test3: true
