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


Java Character toTitleCase()用法及代码示例


Character 类的 toTitleCase(char ch) 方法使用 Unicode 数据文件提供的大小写映射信息将给定的字符参数转换为标题大小写。应该注意的是 Character.isTitleCase(Character.TitleCase(ch)) 对于某些字符可能并不总是返回 true。

已经看到,如果一个字符没有明确的 titlecase 映射并且不是 Unicode Data 文件中的 titlecase 字符,则返回一个大写映射。另一方面,如果字符参数已经是 titlecase 字符,则将返回相同的值。

用法

public static char toTitleCase(char ch)

参数

ch: 就是需要转换的字符。

返回值

toTitleCase(char ch) 方法返回给定字符的标题大小写。否则,此方法返回字符本身。

例子1

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

输出:

The titlecase of character 'b' is given as:B
The titlecase of character 'm' is given as:M

例子2

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

输出:

The titlecase of character ')' is given as:)
The titlecase of character 'G' is given as:G

Java 字符 toTitleCase()Method

Character 类的 toTitleCase(int codePoint) 方法使用由 Unicode 数据文件提供的大小写映射信息将给定的字符(Unicode 代码点)参数转换为标题大小写。

应该注意的是 Character.isTitleCase(Character.TitleCase(codePoint)) 对于某些字符可能并不总是返回 true。

已经看到,如果一个字符没有明确的 titlecase 映射并且不是 Unicode Data 文件中的 titlecase 字符,则返回一个大写映射。另一方面,如果字符参数已经是 titlecase 字符,则将返回相同的值。

用法

public static int toTitleCase(int codePoint)

参数

codePoint:codePoint 是需要测试的字符。

返回值

toTitleCase(int codePoint) 方法返回给定字符(Unicode 代码点)的标题大小写。否则,此方法返回字符本身。

例子1

public class JavaCharacterToTitleCaseExample_1 { 
   public static void main(String[] args) {
      // Create four int primitives.
      int cp1, cp2, cp3, cp4;
      // Assign the values to cp1 and cp2.
      cp1 = 0x0099; 
      cp2 = 0x0080;
      // Assign the titlecase of cp1 and cp2 to cp3 and cp4 respectively.
      cp3 = Character.toTitleCase(cp1);
      cp4 = Character.toTitleCase(cp2);
      String str1 = "The titlecase for the character '" + cp1 + "' isgiven as:" + cp3;
      String str2 = "The titlecase for the character '" + cp2 + "' isgiven as:" + cp4;
      // Print the values of cp3 and cp4.
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出:

The titlecase for the character '153' isgiven as:153
The titlecase for the character '128' isgiven as:128

例子2

public class JavaCharacterToTitleCaseExample_2 { 
   public static void main(String[] args) {
      // Create four int primitives.
      int cp1, cp2, cp3, cp4;
      // Assign the values to cp1 and cp2.
      cp1 = 273; 
      cp2 = 156;
      // Assign the titlecase of cp1 and cp2 to cp3 and cp4 respectively.
      cp3 = Character.toTitleCase(cp1);
      cp4 = Character.toTitleCase(cp2);
      String str1 = "The titlecase for the character '" + cp1 + "' isgiven as:" + cp3;
      String str2 = "The titlecase for the character '" + cp2 + "' isgiven as:" + cp4;
      // Print the values of cp3 and cp4.
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出:

The titlecase for the character '273' isgiven as:272
The titlecase for the character '156' isgiven as:156




相关用法


注:本文由纯净天空筛选整理自 Java Character toTitleCase() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。