当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。