java 字符串 toUpperCase() 方法返回大寫字母的字符串。換句話說,它將字符串的所有字符轉換為大寫字母。
toUpperCase() 方法的用法原理與 toUpperCase(Locale.getDefault()) 方法相同。它在內部使用默認語言環境。
內部實現
public String toUpperCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstLower;
final int len = value.length;
/* Now check if there are any characters that need to be changed. */
scan:{
for (firstLower = 0 ; firstLower < len; ) {
int c = (int)value[firstLower];
int srcCount;
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
c = codePointAt(firstLower);
srcCount = Character.charCount(c);
} else {
srcCount = 1;
}
int upperCaseChar = Character.toUpperCaseEx(c);
if ((upperCaseChar == Character.ERROR)
|| (c != upperCaseChar)) {
break scan;
}
firstLower += srcCount;
}
return this;
}
char[] result = new char[len]; /* may grow */
int resultOffset = 0; /* result may grow, so i+resultOffset
* is the write location in result */
/* Just copy the first few upperCase characters. */
System.arraycopy(value, 0, result, 0, firstLower);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] upperCharArray;
int upperChar;
int srcChar;
int srcCount;
for (int i = firstLower; 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) {
upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
} else {
upperChar = Character.toUpperCaseEx(srcChar);
}
if ((upperChar == Character.ERROR)
|| (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (upperChar == Character.ERROR) {
if (localeDependent) {
upperCharArray =
ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
} else {
upperCharArray = Character.toUpperCaseCharArray(srcChar);
}
} else if (srcCount == 2) {
resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
continue;
} else {
upperCharArray = Character.toChars(upperChar);
}
/* Grow result if needed */
int mapLen = upperCharArray.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] = upperCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)upperChar;
}
}
return new String(result, 0, len + resultOffset);
}
簽名
toUpperCase() 方法有兩種變體。字符串 toUpperCase() 方法的簽名或語法如下:
public String toUpperCase()
public String toUpperCase(Locale locale)
toUpperCase() 的第二種方法變體,使用給定 Locale 的規則將所有字符轉換為大寫。
返回
大寫字母的字符串。
Java String toUpperCase() 方法示例
public class StringUpperExample{
public static void main(String args[]){
String s1="hello string";
String s1upper=s1.toUpperCase();
System.out.println(s1upper);
}}
輸出:
HELLO STRING
Java String toUpperCase(Locale locale) 方法示例 2
import java.util.Locale;
public class StringUpperExample2 {
public static void main(String[] args) {
String s = "hello string";
String turkish = s.toUpperCase(Locale.forLanguageTag("tr"));
String english = s.toUpperCase(Locale.forLanguageTag("en"));
System.out.println(turkish);//will print I with dot on upper side
System.out.println(english);
}
}
輸出:
HELLO STR?NG HELLO STRING
相關用法
- Java String toUpperCase()用法及代碼示例
- Java String toString()用法及代碼示例
- Java String toCharArray()用法及代碼示例
- Java String toLowerCase()用法及代碼示例
- Java String trim()用法及代碼示例
- Java String valueOf()用法及代碼示例
- Java String getChars()用法及代碼示例
- Java String substring()用法及代碼示例
- Java String replace()用法及代碼示例
- Java String contains()用法及代碼示例
- Java String copyValueOf()用法及代碼示例
- Java String isEmpty()用法及代碼示例
- Java String endsWith()用法及代碼示例
- Java String split()用法及代碼示例
- Java String lines()用法及代碼示例
- Java String repeat()用法及代碼示例
- Java String strip()用法及代碼示例
- Java String lastIndexOf()用法及代碼示例
- Java String equals()用法及代碼示例
- Java String replaceAll()用法及代碼示例
注:本文由純淨天空篩選整理自 Java String toUpperCase()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。