62,614
社区成员
发帖
与我相关
我的任务
分享
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
public String toLowerCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstUpper;
/* Now check if there are any characters that need to be changed. */
scan: {
int c;
for (firstUpper = 0 ;
firstUpper < count ;
firstUpper += Character.charCount(c)) {
c = codePointAt(firstUpper);
if (c != Character.toLowerCase(c)) {
break scan;
}
}
return this;//注意这里,如果传入的字符串本身全是小写的话,那么就返回自己,而不是新创建一个String对象,toUpperCase也是一样的,所以s2==s4返回true
}
char[] result = new char[count];
int resultOffset = 0; /* result grows or shrinks, so i+resultOffset
* is the write location in result */
/* Just copy the first few lowerCase characters. */
System.arraycopy(value, offset, result, 0, firstUpper);
String lang = locale.getLanguage().intern();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] lowerCharArray;
int lowerChar;
int srcChar;
int srcCount;
for (int i = firstUpper; i < count; i += srcCount) {
srcChar = codePointAt(i);
srcCount = Character.charCount(srcChar);
if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
} else {
lowerChar = Character.toLowerCase(srcChar);
}
if ((lowerChar == Character.ERROR) ||
Character.isSupplementaryCodePoint(lowerChar)) {
if (lowerChar == Character.ERROR) {
lowerCharArray =
ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
} else {
lowerCharArray = Character.toChars(lowerChar);
}
/* Grow/Shrink result. */
int mapLen = lowerCharArray.length;
char[] result2 = new char[result.length + mapLen - srcCount];
System.arraycopy(result, 0, result2, 0,
i + resultOffset);
for (int x=0; x<mapLen; ++x) {
result2[i+resultOffset+x] = lowerCharArray[x];
}
resultOffset += (mapLen - srcCount);
result = result2;
} else {
result[i+resultOffset] = (char)lowerChar;
}
}
return new String(0, result.length, result);
}