Java-lang-character-tochars-index

提供:Dev Guides
移動先:案内検索

Java.lang.Character.toChars()メソッド

説明

  • java.lang.Character.toChars(int codePoint、char [] dst、int dstIndex)*は、指定された文字(Unicodeコードポイント)をUTF-16表現に変換します。

指定したコードポイントがBMP(Basic Multilingual PlaneまたはPlane 0)値の場合、同じ値がdst [dstIndex]に格納され、1が返されます。

指定されたコードポイントが補助文字の場合、そのサロゲート値はdst [dstIndex](高サロゲート)およびdst [dstIndex + 1](低サロゲート)に格納され、2が返されます。

宣言

以下は* java.lang.Character.toChars()*メソッドの宣言です

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

パラメーター

  • codePoint -Unicodeコードポイント
  • dst -codePointのUTF-16値が格納されているcharの配列
  • dstIndex -変換された値が保存されるdst配列の開始インデックス

戻り値

このメソッドは、コードポイントがBMPコードポイントの場合は1、コードポイントが補助コードポイントの場合は2を返します。

例外

  • IllegalArgumentException -指定されたcodePointが有効なUnicodeコードポイントではない場合
  • NullPointerException -指定されたdstがnullの場合
  • IllegalArgumentException -dstIndexが負であるかdst.length以上の場合、またはdstIndexのdstに結果のchar値を格納するのに十分な配列要素がない場合 (dstIndexがdst.length-1に等しく、指定されたcodePointが補助文字である場合、上位サロゲート値はdst [dstIndex]に格納されません。)

次の例は、lang.Character.toChars()メソッドの使用法を示しています。

package com.finddevguides;

import java.lang.*;

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