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.