Java - Character toChars() 方法



描述

Java 的 Character toChars() 方法將指定的字元(Unicode 程式碼點)轉換為儲存在 char 陣列中的 UTF-16 表示形式。

Unicode 程式碼點分為兩類:基本多語言平面 (BMP) 字元和補充字元。BMP 字元是已用 16 位 Unicode 表示形式表示的普通字元,而補充字元則不是。

如果指定的程式碼點是 BMP 值,則結果 char 陣列的值與 codePoint 相同。如果指定的程式碼點是補充程式碼點,則結果 char 陣列具有相應的代理對。

此方法以兩種多型形式出現,具有不同的引數。

語法

以下是 Java Character toChars() 方法的語法

public static char[] toChars(int codePoint)
(or)
public static int toChars(int codePoint, char[] dst, int dstIndex)

引數

  • codePoint − Unicode 程式碼點

  • dst − char 陣列,其中儲存 codePoint 的 UTF-16 值

  • dstIndex − dst 陣列中儲存轉換值的起始索引

返回值

此方法返回一個 char 陣列,其中包含 codePoint 的 UTF-16 表示形式。

從程式碼點獲取字元陣列示例

以下示例顯示了 Java Character toChars(int codePoint) 方法的使用方法。在此程式中,我們建立了一個 int 變數並將其初始化為一個程式碼點,並使用 toChars() 方法將結果 char 陣列儲存在一個數組中,並列印結果。

package com.tutorialspoint;

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

      // create a char array ch
      char ch[];

      // create an int primitive cp and assign value
      int cp = 0x006e;

      // assign result of toChars on cp to ch
      ch = Character.toChars(cp);
      String str = "Char array having cp's UTF-16 representation is ";
      System.out.print( str );

      // use a for loop to print ch
      for (int i = 0; i < ch.length; i++) {
         System.out.print( ch[i] );
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Char array having cp's UTF-16 representation is n

使用給定索引從字元獲取程式碼點字元陣列示例

以下示例顯示了 Java Character toChars(int codePoint, char[] dst, int dstIndex) 方法的使用方法。在此程式中,我們建立了一個 int 變數並將其初始化為一個程式碼點,並使用 toChars() 方法將結果 char 陣列儲存在一個數組中,並列印結果。

package com.tutorialspoint;

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

      // create an int primitive cp and assign value
      int cp = 0x0036;

      // create an int primitive res
      int res;

      /**
       *  create a char array dst and assign UTF-16 value of cp
       *  to it using toChars method
       */
      char dst[] = Character.toChars(cp);

      // check if cp is BMP or supplementary code point using toChars
      res = Character.toChars(cp, dst, 0);
      String str1 = "It is a BMP code point";
      String str2 = "It is a is a supplementary code point";

      // print res value
      if ( res == 1 ) {
         System.out.println( str1 );
      } else if ( res == 2 ) {
         System.out.println( str2 );
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

It is a BMP code point

獲取程式碼點字元陣列時遇到 IllegalArgumentException 示例

以下示例顯示了 Java Character toChars(int codePoint) 方法的使用方法。在此程式中,我們建立了一個 int 變數並將其初始化為一個程式碼點,並使用 toChars() 方法將結果 char 陣列儲存在一個數組中,並列印結果。

下面給出的示例程式顯示了方法丟擲 IllegalArgumentException 的場景

public class Demo {
   public static void main(String[] args) {
      char ch[];
      int cp = 0x2388211;
      ch = Character.toChars(cp);
      System.out.print("Char array having cp's UTF-16 representation is ");
      for (int i = 0; i < ch.length; i++) {
         System.out.println(ch[i]);
      }
   }
}

異常

上面給出的程式首先被編譯和執行,以產生以下輸出結果。它丟擲一個異常,因為給定方法的輸入是非法的 Unicode 程式碼點。

Exception in thread "main" java.lang.IllegalArgumentException: Not a valid Unicode code point: 0x2388211
        at java.base/java.lang.Character.toChars(Character.java:8573)
        at Demo.main(Demo.java:9)

獲取程式碼點字元陣列時遇到 NullPointerException 示例

以下示例顯示了 Java Character toChars(int codePoint) 方法的使用方法。在此程式中,我們建立了一個 int 變數並將其初始化為一個程式碼點,並使用 toChars() 方法將結果 char 陣列儲存在一個數組中,並列印結果。

在這種情況下,方法丟擲一個 NullPointerException。

public class Demo {
   public static void main(String[] args) {
      int cp = 0x1271;
      int res;      
      res = Character.toChars(cp, null, 0);      
      if ( res == 1 ) {
         System.out.println("It is a BMP code point");
      } else if ( res == 2 ) {
         System.out.println("It is a is a supplementary code point");
      }
   }
}

異常

讓我們編譯並執行上面給出的程式,輸出結果如下:丟擲一個 NullPointerException:

Exception in thread "main" java.lang.NullPointerException
        at java.base/java.lang.Character.toChars(Character.java:8537)
        at Demo.main(Demo.java:11)
java_lang_character.htm
廣告

© . All rights reserved.