Java String toUpperCase()方法

Java String类的toUpperCase()方法将字符串转换为大写字母。换句话说,它将字符串的所有字符转换为大写字母。

toUpperCase()方法与toUpperCase(Locale.getDefault())方法的功能相同。它在内部使用默认语言环境进行转换。

内部实现

public String toUpperCase(Locale locale) {  
      if (locale == null) {  
          throw new NullPointerException();  
      }  
  
      int firstLower;  
      final int len = value.length;  
  
      /* 现在检查是否有任何字符需要更改 */  
      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]; /* 可能会增长 */  
      int resultOffset = 0;  /* 结果可能会增长,所以 i+resultOffset是结果中的写入位置*/  
  
      /* 只复制前几个大写字符 */  
      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);  
              }  
  
              /*如果需要,增长结果*/  
              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()方法根据给定的语言环境将所有字符转换为大写。

返回值

字符串的大写形式。

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);//将在上侧打印带点的 I
        System.out.println(english);  
    }  
}  

输出:

HELLO STR?NG
HELLO STRING

标签: java, Java面试题, Java下载, java教程, java技术, Java学习, Java学习教程, Java语言, Java开发, Java入门教程, Java进阶教程, Java高级教程, Java笔试题, Java编程思想