今天在看源码的时候看到了一个很实用但是却几乎没有人用的一个方法,equalsIgnoreCase ,看了一遍源码后,总结了一下这个方法的用法:用来比较两个字符串,并且忽略其大小的情况,判断被比较的字符串中是否包含其中的字符串,这个可以广泛的运用到java的爬虫中,可以节省很多代码量和效率。
首先来看看源码,先进行字符串的判空,然后进行一个regionmatches的比较,
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true : (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}
下面是regionMatches中的源码,这个方法才是真正的底层,看完了就懂了。
public boolean regionMatches(boolean ignoreCase, int toffset,
String other, int ooffset, int len) {
char ta[] = value;
int to = toffset;
char pa[] = other.value;
int po = ooffset;
// Note: toffset, ooffset, or len might be near -1>>>1.
if ((ooffset < 0) || (toffset < 0)
|| (toffset > (long)value.length - len)
|| (ooffset > (long)other.value.length - len)) {
return false;
}
while (len-- > 0) {
char c1 = ta[to++];
char c2 = pa[po++];
if (c1 == c2) {
continue;
}
if (ignoreCase) {
// If characters don't match but case may be ignored,
// try converting both characters to uppercase.
// If the results match, then the comparison scan should
// continue.
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
// Unfortunately, conversion to uppercase does not work properly
// for the Georgian alphabet, which has strange rules about case
// conversion. So we need to make one last check before
// exiting.
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
return false;
}
return true;
}
再举个使用的场景例子:判定字符串出现的次数
public static void main(String[] args) {
/**@author yemaomao
* @param 判断字符串出现的次数
*/
int number = 0;
String str = "fdafdadfadf";
for (int i = 0; i < str.length(); i++) {
if (str.regionMatches(i, "da", 0, 2)) {
number++;
}
}
System.out.println(number);
}
}