其他
Java教程-Java String toLowerCase()方法
整理:Java面试那些事儿
Java String类的toLowerCase()方法返回字符串的小写形式。换句话说,它将字符串的所有字符转换为小写字母。
toLowerCase()方法的工作方式与toLowerCase(Locale.getDefault())方法相同。它在内部使用默认的语言环境(locale)。
专属福利
内部实现
public String toLowerCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstUpper;
final int len = value.length;
/* 现在检查是否有任何字符需要更改 */
scan: {
for (firstUpper = 0 ; firstUpper < len; ) {
char c = value[firstUpper];
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
int supplChar = codePointAt(firstUpper);
if (supplChar != Character.toLowerCase(supplChar)) {
break scan;
}
firstUpper += Character.charCount(supplChar);
} else {
if (c != Character.toLowerCase(c)) {
break scan;
}
firstUpper++;
}
}
return this;
}
char[] result = new char[len];
int resultOffset = 0; /* 结果可能会增长,所以 i+resultOffset
* i 是结果中的写入位置 */
/* 只复制前几个小写字符*/
System.arraycopy(value, 0, result, 0, firstUpper);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] lowerCharArray;
int lowerChar;
int srcChar;
int srcCount;
for (int i = firstUpper; i < len; i += srcCount) {
srcChar = (int)value[i];
if ((char)srcChar >= Character.MIN_HIGH_SURROGATE
&& (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
srcChar = codePointAt(i);
srcCount = Character.charCount(srcChar);
} else {
srcCount = 1;
}
if (localeDependent || srcChar == '\u03A3') { // 希腊大写字母 SIGMA
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
} else if (srcChar == '\u0130') { // 拉丁文大写字母 I DOT
lowerChar = Character.ERROR;
} else {
lowerChar = Character.toLowerCase(srcChar);
}
if ((lowerChar == Character.ERROR)
|| (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (lowerChar == Character.ERROR) {
if (!localeDependent && srcChar == '\u0130') {
lowerCharArray =
ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);
} else {
lowerCharArray =
ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
}
} else if (srcCount == 2) {
resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
continue;
} else {
lowerCharArray = Character.toChars(lowerChar);
}
/* 如果需要,增长结果 */
int mapLen = lowerCharArray.length;
if (mapLen > srcCount) {
char[] result2 = new char[result.length + mapLen - srcCount];
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for (int x = 0; x < mapLen; ++x) {
result[i + resultOffset + x] = lowerCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)lowerChar;
}
}
return new String(result, 0, len + resultOffset);
}
语法
toLowerCase()方法有两个变体。下面是字符串toLowerCase()方法的语法或签名:
public String toLowerCase()
public String toLowerCase(Locale locale)
第二个变体的toLowerCase()方法使用给定语言环境(locale)的规则将所有字符转换为小写字母。
返回值
小写形式的字符串。
Java String toLowerCase()方法示例
public class StringLowerExample{
public static void main(String args[]){
String s1="JAVATPOINT HELLO stRIng";
String s1lower=s1.toLowerCase();
System.out.println(s1lower);
}}
输出:
javatpoint hello string
Java String toLowerCase(Locale locale)方法示例2
该方法允许我们传递语言环境(locale),以获取不同语言的字符串。让我们看一个例子,其中我们获得英语和土耳其语的字符串。
import java.util.Locale;
public class StringLowerExample2 {
public static void main(String[] args) {
String s = "JAVATPOINT HELLO stRIng";
String eng = s.toLowerCase(Locale.ENGLISH);
System.out.println(eng);
String turkish = s.toLowerCase(Locale.forLanguageTag("tr")); // It shows i without dot
System.out.println(turkish);
}
}
输出:
javatpoint hello string
javatpo?nt hello str?ng
我就知道你会点赞+“在看”