Java Character.isValidCodePoint()方法

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

Java isValidCodePoint()方法是Character类的一部分。此方法用于检查指定的 Unicode 代码点是否是有效的 Unicode 代码点值。

语法:

public static boolean isValidCodePoint(int codePoint)

参数:

传递的参数是要检查其是否有效的 Unicode 代码点。

返回:

如果指定的代码点值在MIN_CODE_POINTMAX_CODE_POINT之间(含这两个值),则返回布尔值true ,否则返回false

例 1:

这里,检查字符是否是有效的代码点值。

public class StudyTonight
{  
    public static void main(String[] args)
    {  
        int cp1 = 73;  
        int cp2 = 60;  
        int cp3 = 119;  
        int cp4 = 0x0123;   
        int cp5 = 0x123fff;  

        boolean b1 = Character.isValidCodePoint(cp1);  
        boolean b2 = Character.isValidCodePoint(cp2);  
        boolean b3 = Character.isValidCodePoint(cp3);  
        boolean b4 = Character.isValidCodePoint(cp4);  
        boolean b5 = Character.isValidCodePoint(cp5);  

        System.out.println((char)cp1 +" is valid codepoint value??::  "+b1);  
        System.out.println((char)cp2 +" is valid codepoint value??::  "+b2);  
        System.out.println((char)cp3 +" is valid codepoint value??::  "+b3);  
        System.out.println((char)cp4 +" is valid codepoint value??::  "+b4);  
        System.out.println((char)cp5 +" is valid codepoint value??::  "+b5);  
    }  
}

I 是有效码点值??真 <是否为有效码点值?? true w 是有效的代码点值??真 ?是有效的代码点值吗??真 ?是有效的代码点值吗??*假的

例 2:

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

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

输入 Unicode 字符:11 是有效的码位吗??:真

                                                            • *输入 Unicode 字符:787 ?是有效的代码点吗??:真

实时示例:

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