Java Character.toTitleCase(int)方法

原文:https://www.studytonight.com/java-wrapper-class/java-character-totitlecaseint-codepoint-method

Java toTitleCase(int codePoint)方法是Character类的一部分。此方法使用 Unicode 数据文件中的大小写映射信息,将指定的 Unicode 代码点字符参数转换为 titlecase。

必须注意的是,如果代码点字符没有显式的标题大小写映射,并且根据 UnicodeData,它本身不是标题大小写字符,那么大写映射将作为等效的标题大小写映射返回。如果 char 参数已经是 titlecase 字符,将返回相同的字符值。

语法:

public static char toTitleCase(int codePoint)

参数:

传递的参数是要转换为 titlecase 的 Unicode 代码点字符值。

返回:

返回指定代码点字符的标题大小写值。

例 1:

在这里,字符被转换成等价的标题字符。

public class StudyTonight
{  
    public static void main(String[] args)
    {  
        int cp1 = 78;  
        int cp2 = 102;  
        int cp3 = 66;  
        int cp4 = 48;   
        int cp5 = 1232;  

        char ch1 = Character.toTitleCase(cp1);  
        char ch2 = Character.toTitleCase(cp2);  
        char ch3 = Character.toTitleCase(cp3);  
        char ch4 = Character.toTitleCase(cp4);  
        char ch5 = Character.toTitleCase(cp5);  

        System.out.println("The titlecase character is :"+ch1);  
        System.out.println("The titlecase character is :"+ch2); 
        System.out.println("The titlecase character is :"+ch3);  
        System.out.println("The titlecase character is :"+ch4);  
        System.out.println("The titlecase character is :"+ch5);  
    }  
}

片头字为:N 片头字为:F 片头字为:B 片头字为:0 片头字为:?

例 2:

这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得等效的输出。

import java.util.Scanner; 
public class StudyTonight
{  
    public static void main(String[] args)
    {  
        try
        {
            System.out.print("Enter the Unicode codepoint: ");  
            Scanner sc = new Scanner(System.in);         
            int cp = sc.nextInt();  
            char cc = Character.toTitleCase(cp);
            System.out.println("The titlecase character is : "+cc);
        }
        catch(Exception e)
        {
            System.out.println("Invalid Input!!");
        }
    }  
}

输入 Unicode 码位:99 片头字符为:C


输入 Unicode 码位:110 片头字符为:N

实时示例:

在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。