Java Character.getType(int)方法

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

Java getType(int codepoint)方法是Character类的一部分。此方法返回一个值,该值指示指定的 Unicode 代码点字符值的常规类别。

注:该方法适用于补充字符,也适用于非补充字符。

语法:

public static int getType(int codePoint)

参数:

传递的参数是 Unicode 代码点字符,它的常规类别值将被返回。

返回:

返回表示指定 Unicode 代码点字符的常规类别的整数值。

例 1:

这里,返回代表指定 Unicode 代码点字符的一般类别的整数值。

public class StudyTonight 
{
    public static void main(String[] args) 
    {
        int cp1 = 88;
        int cp2 = 2;
        int cp3 = 62;
        int cp4 = 101;
        int cp5 = 84;
        int n1 = Character.getType(cp1);
        int n2 = Character.getType(cp2);
        int n3 = Character.getType(cp3);
        int n4 = Character.getType(cp4);
        int n5 = Character.getType(cp5);
        System.out.println("The value of " + (char) cp1 + " is : " + n1);
        System.out.println("The value of " + (char) cp2 + " is : " + n2);
        System.out.println("The value of " + (char) cp3 + " is : " + n3);
        System.out.println("The value of " + (char) cp4 + " is : " + n4);
        System.out.println("The value of " + (char) cp5 + " is : " + n5);
    }
}

X 的值为:1 的值为:15 的值为:25 的值为:2 的值为:1

例 2:

这里有一个通用的例子,用户可以输入他选择的输入并获得所需的输出。

import java.util.Scanner;
public class StudyTonight 
{
    public static void main(String[] args) 
    {
        try 
        {
            System.out.println("Enter the unicode codepoint character");
            Scanner sc = new Scanner(System. in );
            int cp = sc.nextInt();
            int r = Character.getType(cp);
            System.out.println("The character " + (char) cp + " has type value : " + r);
        }
        catch(Exception e) 
        {
            System.out.println("Invalid input");
        }
    }
}

输入 unicode 码点字符 33 这个字符!有类型值:24

                                                                                      • T4】输入 unicode 码点字符 121 字符 y 有类型值:2

实时示例:

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