Java Character.codePointOf()方法

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

Java codePointOf()方法是Character类的一部分。此方法返回由给定的 Unicode 字符名称指定的 Unicode 字符的代码点值。

语法:

public static int codePointOf(String name)

参数:

传递的参数是要返回其代码点的 Unicode 字符名称。

返回:

返回参数指定的字符的代码点值。

例外情况:

  • IllegalArgumentException -如果指定的name不是有效的 Unicode 字符名称。
  • NullPointerException -如果namenull

例 1:

这里,传递指定的名称,并返回指定的代码点值。

import java.lang.Character;
public class StudyTonight
{    
    public static void main(String [] args)throws Exception
    {             
        int cp1 = Character.codePointOf("LATIN SMALL LETTER A WITH DIAERESIS");  
        System.out.println("The codePoint is : "+cp1);  

        int cp2 = Character.codePointOf("RIGHT SQUARE BRACKET");  
        System.out.println("The codePoint is : "+cp2);

        int cp3 = Character.codePointOf("LATIN CAPITAL LETTER E");  
        System.out.println("The codePoint is : "+cp3);

        int cp4 = Character.codePointOf("DIGIT SEVEN");  
        System.out.println("The codePoint is : "+cp4);
    }   
}

码点为:228 码点为:93 码点为:69 码点为:55

例 2:

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

import java.lang.Character;
import java.util.Scanner;
public class StudyTonight
{    
    public static void main(String [] args)throws Exception
    {   
        try
        {
            System.out.println("Enter Name:");
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();

            System.out.println("The codepoint count is : " +Character.codePointOf(s));            
        }
        catch(Exception e)
        {
            System.out.println("Invalid Input");
        } 
    }    
}

输入姓名: 数字八 码点计数为:56

                                                                          • T4】输入姓名: 左方括号 码点计数为:91
                                                                • *输入姓名: 莫希特 无效