當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Character toUpperCase()用法及代碼示例


Character 類的 toUpperCase(char ch) 方法使用 Unicode 數據文件提供的大小寫映射信息將給定的字符參數轉換為大寫。

應該注意的是 Character.isUpperase(Character.UpperCase(ch)) 對於某些字符可能並不總是返回 true。

實際上,可以使用 String.toUpperCase() 將字符映射為大寫。與字符大小寫映射相比,字符串大小寫映射有很多好處。字符串大小寫映射可用於執行 local-sensitive 映射、context-sensitive 映射,而不能使用字符大小寫映射。

用法

public static char toUpperCase(char ch)

參數

ch: 就是需要轉換的字符。

返回值

toUpperCase(char ch) 方法返回給定字符的大寫。否則,此方法返回字符本身。

例子1

public class JavaCharacterToUpperCaseExample1 {
    public static void main(String[] args) {
      // Create four char primitives.
        char ch1, ch2, ch3, ch4;
      // Assign the values to ch1 and ch2.
      ch1 = 'm';
      ch2 = 'q';
      // Assign the uppercase of ch1 and ch2 to ch3 and ch4 respectively.
      ch3 = Character.toUpperCase(ch1);
      ch4 = Character.toUpperCase(ch2);
      String str1 = "The uppercase of the character '" + ch1 + "' is given as:" + ch3;
      String str2 = "The uppercase of the character '" + ch2 + "' is given as:" + ch4;
      // Print the values of ch1 and ch2.
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出:

The titlecase of character 'm' is given as:M
The titlecase of character 'q' is given as:Q

例子2

public class JavaCharacterToUpperCaseExample2{
    public static void main(String[] args) {
      // Create four char primitives.
        char ch1, ch2, ch3, ch4;
      // Assign the values to ch1 and ch2.
      ch1 = '+';
      ch2 = 'f';
      // Assign the uppercase of ch1 and ch2 to ch3 and ch4 respectively.
      ch3 = Character.toUpperCase(ch1);
      ch4 = Character.toUpperCase(ch2);
      String str1 = "The uppercase of the character '" + ch1 + "' is given as:" + ch3;
      String str2 = "The uppercase of the character '" + ch2 + "' is given as:" + ch4;
      // Print the values of ch1 and ch2..
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出:

The uppercase of the character '+' is given as:+
The uppercase of the character 'F' is given as:F

Java Character toUpperCase(int codePoint) 方法

Character 類的 toUpperCase(int codePoint) 方法使用由 Unicode 數據文件提供的大小寫映射信息將給定的字符(Unicode 代碼點)參數轉換為大寫。

需要注意的是 Character.isUpperase(Character.UpperCase(codePoint)) 對於某些字符可能並不總是返回 true。

實際上,可以使用 String.toUpperCase() 將字符映射為大寫。與字符大小寫映射相比,字符串大小寫映射有很多好處。字符串大小寫映射可用於執行 local-sensitive 映射、context-sensitive 映射,而不能使用字符大小寫映射。

用法

public static int toUpperCase(int codePoint)

參數

codePoint:codePoint 是需要測試的字符。

返回值

toUpperCase(int codePoint) 方法返回給定字符(Unicode 代碼點)的大寫。否則,此方法返回字符本身。

例子1

public class JavaCharactertoUpperCaseExample_1 {
    public static void main(String[] args) {
	// Initialize two char primitives.
           int codepoint1 = 102;
           int codepoint2 = 110;
        // Convert the codepoints to char type.
	    char ch1 = (char) codepoint1;
           char ch2 = (char) codepoint2;
	// Convert the codepoints to uppercase.
	   char upper1 = Character.toUpperCase(ch1);
          char upper2 = Character.toUpperCase(ch2);
	// Print the result.
System.out.println("The uppercase for the character '" + ch1 + "' is given as:" + upper1);
System.out.println("The uppercase for the character '" + ch2 + "' is given as:" + upper2);
	}
}

輸出:

The uppercase for the character 'f' is given as:F
The uppercase for the character 'n' is given as:N

例子2

public class JavaCharactertoUpperCaseExample_2 {
    public static void main(String[] args) {
         // Initialize two char primitives.
           int codepoint1 = 119;
           int codepoint2 = 80;
        // Convert the codepoints to char type.
	   char ch1 = (char) codepoint1;
           char ch2 = (char) codepoint2;
	// Convert the codepoints to uppercase.
	   char upper1 = Character.toUpperCase(ch1);
           char upper2 = Character.toUpperCase(ch2);
	// Print the result.
String str1 = "The uppercase for the character '" + ch1 + "' is given as:" + upper1;
String str2 = "The uppercase for the character '" + ch2 + "' is given as:" + upper2;
	 System.out.println(str1);
               System.out.println(str2);
    }
}

輸出:

The uppercase for the character 'w' is given as:W
The uppercase for the character 'P' is given as:P




相關用法


注:本文由純淨天空篩選整理自 Java Character toUpperCase() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。