Java String toUpperCase()

toUpperCase() 方法将字符串小写字符转换为大写。

1 语法

public String toUpperCase()  

或

public String toUpperCase(Locale locale)  

2 参数

3 返回值

字符转换为大写后的字符串。

4 toUpperCase()内部源码

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);  
  }

5 toUpperCase()示例

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java String.toUpperCase方法的例子
 */
public class Demo{

    public static void main(String args[]){
        String s1="hello string";
        String s1upper=s1.toUpperCase();
        System.out.println(s1upper);
    }
}

输出结果为:

HELLO STRING

6 toUpperCase(Locale locale)示例

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java String.toUpperCase方法的例子
 */
import java.util.Locale;

public class Demo {

    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);
        System.out.println(english);
    }
}

输出结果为:

HELLO STRİNG
HELLO STRING

 

热门文章

优秀文章