Java 为什么equalsIgnoreCase()不';你不能只使用toLowerCase()吗?
为什么equalsIgnoreCase()首先使用toUpperCase()和toLowerCase()来避免错误识别格鲁吉亚字母表,而不是第一次使用toLowerCase()并且只使用它Java 为什么equalsIgnoreCase()不';你不能只使用toLowerCase()吗?,java,Java,为什么equalsIgnoreCase()首先使用toUpperCase()和toLowerCase()来避免错误识别格鲁吉亚字母表,而不是第一次使用toLowerCase()并且只使用它 public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) { char ta[] = value; int to = toffset;
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 boolean region匹配(boolean ignoreCase,int-toffset,
字符串其他,int ooffset,int len){
char ta[]=值;
int-to=toffset;
char pa[]=其他值;
int po=偏移量;
//注意:toffset、ooffset或len可能接近-1>>>1。
如果((ooffset<0)| |(toffset<0)
||(toffset>(长)value.length-len)
||(ooffset>(long)other.value.length-len){
返回false;
}
而(长度-->0){
char c1=ta[to++];
char c2=pa[po++];
如果(c1==c2){
继续;
}
如果(忽略案例){
//如果字符不匹配但大小写可能被忽略,
//尝试将两个字符都转换为大写。
//如果结果匹配,则应进行比较扫描
//继续。
char u1=Character.toUpperCase(c1);
char u2=Character.toUpperCase(c2);
如果(u1==u2){
继续;
}
//不幸的是,转换为大写不能正常工作
//对于格鲁吉亚字母表,它有奇怪的大小写规则
//转换。所以我们需要在转换前做最后一次检查
//退出。
if(Character.toLowerCase(u1)=Character.toLowerCase(u2)){
继续;
}
}
返回false;
}
返回true;
}
注意,它在toupercase
的结果(u1
和u2
)上使用了toLowerCase
,而不是在原始字符(c1
和c2
)上。它检查的是c1
转换为大写(u1
)然后转换为小写匹配c2
转换为大写(u2
)然后转换为小写。因此,它不能只跳过开头的toupercase
,它使用的是从中获得的信息(u1
和u2
)。没有证据排除其他字符集在转换为小写时不能正常工作。他们似乎只是为了安全起见。谢谢,我在重复的问题中找到了答案。