描述
java.lang.String.regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 方法测试,如果两个字符串的区域都是平等的。此String对象的一个子相比,其他的参数的子串
其结果是真实的,如果这些子表示的字符序列是相同的,忽略大小写的情况下,当且仅当ignoreCase为true。此String对象的子字符串:进行比较在indextoffset开始和长度len。其他的子串的:进行比较开始在指数ooffset和长度为len
声明
以下是声明java.lang.String.regionMatches()方法
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
参数
-
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.
返回值
此方法返回true,如果该字符串指定的次区域相匹配的字符串参数指定的次区域,否则返回false
异常
-
NA
实例
下面的例子显示使用的java.lang.String.regionMatches()方法
package com.yiibai; 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); } }
让我们来编译和运行上面的程序,这将产生以下结果:
region matched = true region matched = true