當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Character toChars()用法及代碼示例


Character 類的 toChars(int codePoint) 方法通常將指定字符轉換為其 UTF-16 表示,該表示通常存儲在 char 數組中。如果指定的 codePoint 是 BMP 值,則生成的 char 值數組與 codePoint 具有相同的值。另一方麵,如果指定的 codePoint 是補充 codePoint,則生成的 char 數組包含相應的代理項對。

用法

public static char[] toChars(int codePoint)

參數

codePoint:codePoint 是需要測試的字符。

返回值

toChars(int codePoint) 方法返回具有字符的 UTF-16 表示的 char 數組。

例子1

public class JavaCharcatertoCharsExample1 {
   public static void main(String[] args) {
      // Create a char array ch.
      char ch[];
      // Create an integer primitive cp and assign the value.
      int cp = 0x006e;
      // Assign the result of integer primitive cp to ch.
      ch = Character.toChars(cp);
      String str = "The char array with the UTF-16 representation is given as:";
      System.out.print( str );
      // Consider a for loop to print the value for ch.
      for (int i = 0; i < ch.length; i++) {
         System.out.print( ch[i] );
            }  
     }
}

輸出:

The char array with the UTF-16 representation is given as:n

例子2

public class JavaCharcatertoCharsExample2 {
   public static void main(String[] args) {
       // Create a char array ch.
      int cp = 0x006f;
      // Create a char primitive ch and assign the value.
      char[] ch = Character.toChars(cp);
      // Print the values.
      System.out.print( "The char array with the UTF-16 representation is given as:" );
      for (int i=0; i<ch.length; i++){
         System.out.print( ch[i] );
      }
   }
}

輸出:

The char array with the UTF-16 representation is given as:o

Java 字符 toChars(int codePoint, char[] dst, int dstIndex) 方法

Character 類的 toChars(int codePoint, char[] dst, int dstIndex) 方法將指定的字符轉換為它們各自的 UTF-16 表示。

如果指定的codePoint為BMP(Basic Multilingual Plane) codePoint值,則獲取的值存儲在dstIndex中,返回1。另一方麵,如果指定的codePoint為增補字符,則將獲取的代理值存入dstIndex,返回1。

用法

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

參數

上述方法需要三個參數:

a.)codePoint 是需要測試的字符。

b.) dst 是一個字符數組,其中存儲了 codePoint 的 UTF-16 值。

c.)dstIndex 通常是存儲轉換值的 dst 數組的起始索引。

返回

如果所需的代碼點是 BMP 代碼點,則 toChars(int codePoint, char[] dst, int dstIndex) 方法返回 1。否則,如果所需的代碼點是補充代碼點,則該方法返回 2。

例子1

public class JavaCharactertoCharsExample1 {
    public static void main(String[] args) {
      // Create two integer primitives cp1 and cp2 and then assign the values.
      int cp1 = 0x0090;
      int cp2 = 0x0044;
      // Create two integer primitives result1 and result2.
      int result1, result2;
       // Create two char array ch1 and ch2.
      char ch1[] = Character.toChars(cp1);
      char ch2[] = Character.toChars(cp2);

      // Check whether the primitives are BMP or supplementary code point using toChars().
      result1 = Character.toChars(cp1, ch1, 0);
      result2 = Character.toChars(cp2, ch2, 0);

      String str1 = "The primitive is a BMP code point.";
      String str2 = "The primitive is a supplementary code point.";

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

輸出:

The primitive is a BMP code point.
The primitive is a BMP code point.

例子2

public class JavaCharactertoCharsExample2 {
    public static void main(String[] args) {
     int cp1 = 121;

     char ch1[] = Character.toChars(cp1);
     
      int result1 = Character.toChars(cp1, ch1, 0);
     
      String str1 = "The primitive is a BMP code point.";
      String str2 = "The primitive is a supplementary code point.";

      if ( result1 == 1 ){
         System.out.println( str1);
      }
      else if ( result1 == 2 ){
         System.out.println( str2);
         }
     }
}

輸出:

The primitive is a BMP code point.




相關用法


注:本文由純淨天空篩選整理自 Java Character toChars() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。