当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。