Java - Character isValidCodePoint() 方法



描述

Java 的 Character isValidCodePoint() 方法用于确定一个代码点值是否为有效的 Unicode 代码点值。范围在 U+0000 到 U+10FFFF 之间的数值称为字符的有效代码点。

在 Unicode 系统中,代码点被定义为字符(char 数据类型)的数值等价物(整数数据类型)。

它们分为两类:基本多语言平面 (BMP) 字符和补充字符。BMP 字符是一般字符,可以用 16 位 Unicode 系统表示,而补充字符则不能用 16 位 Unicode 系统表示。

BMP 字符的范围是 U+0000 到 U+FFFF,补充字符的范围是 U+10000 到 U+10FFFF。

语法

以下是 Java Character isValidCodePoint() 方法的语法

public static boolean isValidCodePoint(int codePoint)

参数

  • codePoint − 要测试的 Unicode 代码点

返回值

如果指定的代码点值是有效的代码点(包含在 MIN_CODE_POINT 和 MAX_CODE_POINT 之间),则此方法返回 true,否则返回 false。

检查代码点是否为有效 Unicode 代码点示例

以下示例演示了 Java Character isValidCodePoint() 方法的使用。在此程序中,我们创建了两个 int 变量并为它们分配了两个十六进制值。然后使用 isValidCodePoint() 方法检查 int 变量的状态,并打印结果。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x0123;
      cp2 = 0x123fff;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if cp1, cp2 are valid Unicode code points 
       *  and assign results to b1, b2
       */
      b1 = Character.isValidCodePoint(cp1);
      b2 = Character.isValidCodePoint(cp2);
      String str1 = "cp1 is a valid Unicode code point is " + b1;
      String str2 = "cp2 is a valid Unicode code point is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

cp1 is a valid Unicode code point is true
cp2 is a valid Unicode code point is false

检查字符是否为有效 Unicode 代码点示例

由于代码点是字符值的数值等价物,因此我们可以对字符变量使用类型转换技术并将其转换为 int 值。此 int 值作为参数传递给方法。

package com.tutorialspoint;

public class CodePointDemo {
   public static void main(String args[]) {
      char ch = 's';
      boolean b = Character.isValidCodePoint((int)ch);
      System.out.println(ch + " is a valid code point: " + b);
   }
}

输出

获得的输出如下:

s is a valid code point: true

检查代码点是否为有效 Unicode 代码点示例

由于此方法的返回类型为布尔型,因此我们在本示例中使用条件语句来检查作为参数传递给方法的代码点是否为有效代码点。

package com.tutorialspoint;

public class CodePointDemo {
   public static void main(String args[]) {
      int ch = 0x0012;
      if(Character.isValidCodePoint(ch))
         System.out.println(ch + " is a valid code point");
      else
         System.out.println(ch + " is not a valid code point");
   }
}

输出

编译并运行上述程序后,将显示如下输出:

18 is a valid code point

检查代码点是否为有效 Unicode 代码点示例

在以下示例中,我们使用循环语句将代码点数组中的元素作为参数传递给方法,并检查这些值是否有效。

package com.tutorialspoint;

public class CodePointDemo {
   public static void main(String args[]) {
      int ch[] = {0x0012, 223, 0x15FF};
      for(int i = 0; i < ch.length; i++) {
         if(Character.isValidCodePoint(ch[i]))
            System.out.println(ch[i] + " is a valid code point");
         else
            System.out.println(ch[i] + " is not a valid code point");
      }
   }
}

输出

编译并运行上述程序后,获得的输出如下:

18 is a valid code point
223 is a valid code point
5631 is a valid code point
java_lang_character.htm
广告

© . All rights reserved.