📜  Java中的字符 .isValidCodePoint() 方法及示例

📅  最后修改于: 2022-05-13 01:55:25.912000             🧑  作者: Mango

Java中的字符 .isValidCodePoint() 方法及示例

字符.isValidCodePoint()是Java中的一个内置方法,用于确定参数中提到的指定代码点是否为有效的 Unicode 代码点值。

句法:

public static boolean isValidCodePoint(int codePoint)


参数:
参数codePoint是Integer数据类型,是指要测试的unicode码点。

返回值:如果指定的代码点值介于 MIN_CODE_POINT 和 MAX_CODE_POINT 之间,则此方法返回 true,否则返回 false。

下面的程序说明了字符.isValidCodePoint() 方法的使用:
方案一:

// Java program to demonstrate the
// Character.isValidCodePoint() method
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // Create 2 int primitives c1, c2 and assign values
        int c1 = 0x0125, c2 = 0x123fff;
  
        boolean bool1 = Character.isValidCodePoint(c1);
        boolean bool2 = Character.isValidCodePoint(c2);
  
        String str1 = "c1 is a valid Unicode code point is " + bool1;
        String str2 = "c2 is a valid Unicode code point is " + bool2;
  
        // Print bool1, bool2 values
        System.out.println(str1);
        System.out.println(str2);
    }
}
输出:
c1 is a valid Unicode code point is true
c2 is a valid Unicode code point is false

方案二:

// Java program to demonstrate the
// Character.isValidCodePoint() method 
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // Create 2 int primitives c1, c2 and assign values
        int c1 = 0x0128, c2 = 0x123ddd;
  
        boolean bool1 = Character.isValidCodePoint(c1);
        boolean bool2 = Character.isValidCodePoint(c2);
  
        String str1 = "c1 is a valid Unicode code point is " + bool1;
        String str2 = "c2 is a valid Unicode code point is " + bool2;
  
        // Print bool1, bool2 values
        System.out.println(str1);
        System.out.println(str2);
    }
}
输出:
c1 is a valid Unicode code point is true
c2 is a valid Unicode code point is false

参考:https: Java/lang/ 字符.html#isValidCodePoint(int)