Java - Character isSupplementaryCodePoint() 方法



说明

Java Character isSupplementaryCodePoint() 方法确定指定的字符(Unicode 字符)是否处于补充字符范围内。Unicode 系统中的补充字符范围在 U+10000 到 U+10FFFF。

Unicode 码点分为两类 - 基本多语言平面 (BMP) 码点和补充码点。BMP 码点存在于 U+0000 到 U+FFFF 范围内。

然而,补充字符是稀有字符,不使用原始 16 位 Unicode 表示。例如,这些类型的字符用于中文或日文脚本,因此这些国家/地区使用的应用程序需要这些字符。

语法

以下是对 Java Character isSupplementaryCodePoint() 方法的语法

public static boolean isSupplementaryCodePoint(int codePoint)

参数

  • codePoint - 要测试的 Unicode 码点

返回值

此方法返回 true,如果指定的码点落在补充码点范围内(MIN_SUPPLEMENTARY_CODE_POINT 到 MAX_CODE_POINT 包含),否则返回 false。

检查 CodePoint 是否是补充 CodePoint 的示例

以下示例展示了 Java Character isSupplementaryCodePoint() 方法的用法。在该示例中,我们创建了两个 int 变量,并为它们分配了两个码点。然后,使用 isSupplementaryCodePoint() 方法,我们检查补充状态,并打印结果。

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 = 0x10ffd;
      cp2 = 0x004ca;

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

      /**
       *  check if cp1, cp2 are in supplementary character range
       *  and assign results to b1, b2
      */
      b1 = Character.isSupplementaryCodePoint(cp1);
      b2 = Character.isSupplementaryCodePoint(cp2);
      String str1 = "cp1 represents a supplementary code point is " + b1;
      String str2 = "cp2 represents a supplementary code point is " + b2;

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

输出

让我们编译并运行以上程序,它会生成以下结果 -

cp1 represents a supplementary code point is true
cp2 represents a supplementary code point is false

检查 CodePoint 是否是补充 CodePoint 的示例

由于该方法返回布尔值,因此我们还可以使用此方法作为 Java 中条件语句(if - else)的条件。

package com.tutorialspoint;

public class Demo{   
   public static void main(String []args){
      int cp = 0x10023;
      if(Character.isSupplementaryCodePoint(cp))
         System.out.println("The character is a supplementary character");
      else
         System.out.println("The character is not a supplementary character");
   }
}

输出

程序编译并执行后,输出显示如下 -

The character is a supplementary character

Checking a CodePoint to be Supplementary CodePoint Example

In addition to the conditional statements, we can use loop statements on an integer array containing code points. We use loop statements to traverse through the array and then, in conditional statements, the method is used as a condition for each code point to check whether they are supplementary characters or not.

package com.tutorialspoint;

public class CharacterDemo {    
   public static void main(String []args){
      int cp[] = new int[] {0x6718, 0x10034, 0x10fff, 0x07821, 0x3468};
      for(int i = 0; i < cp.length; i++) {
         if(Character.isSupplementaryCodePoint(cp[i]))
            System.out.println("The character is a supplementary character");
         else
            System.out.println("The character is not a supplementary character");
      }
   }
}

Output

The output is achieved after compiling and executing the program as follows −

The character is not a supplementary character
The character is a supplementary character
The character is a supplementary character
The character is not a supplementary character
The character is not a supplementary character
java_lang_character.htm
Advertisements
© . All rights reserved.