Java - Character codePointAt() 方法



描述

Java 的Character codePointAt()方法將檢索字元陣列中指定索引處的字元的程式碼點。這些程式碼點可以表示普通字元和補充字元。

當字元陣列中特定索引處的值滿足以下條件時,將獲取補充程式碼點字元:

  • 如果字元值屬於高代理範圍 (U+D800 到 U+DBFF)。

  • 如果給定索引的下一個索引小於字元陣列的長度。

  • 如果此下一個索引中的值屬於低代理範圍 (U+DC00 到 U+DFFF)。

在任何其他情況下,將獲取普通程式碼點字元。

注意 - 此方法以三種不同的多型形式出現,並具有不同的引數。

語法

以下是 Java Character codePointAt() 方法的各種語法

public static int codePointAt(char[] a, int index)
(or)
public static int codePointAt(char[] a, int index, int limit)
(or)
public static int codePointAt(CharSequence seq, int index)

引數

  • a - 字元陣列

  • index - 要轉換的字元陣列中字元值(Unicode 程式碼單元)的索引

  • limit - 字元陣列中最後一個可使用的陣列元素之後的索引。

  • seq - 一系列字元值(Unicode 程式碼單元)

返回值

此方法返回字元陣列/CharSequence 引數給定索引處的 Unicode 程式碼點。

獲取字元陣列中給定索引處的字元的程式碼點示例

以下示例演示了 Java Character codePointAt() 方法的使用。當我們將一個簡單的字元陣列和一個指定的小於陣列長度的索引作為方法的引數時,返回值將是該索引處存在的字元的程式碼點。

package com.tutorialspoint;

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

      // create a char array c and assign values
      char[] c = new char[] { 'a', 'b', 'c', 'd', 'e' };

      // create 2 int's res, index and assign value to index
      int res, index  = 0;

      // assign result of codePointAt on array c at index to res
      res = Character.codePointAt(c, index);
      String str = "Unicode code point is " + res;

      // print res value
      System.out.println( str );
   }
}

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

Unicode code point is 97

獲取字元陣列中每個字元的程式碼點示例

在下面的另一個示例中,我們使用迴圈語句來檢索陣列中每個元素的程式碼點。透過使用迴圈語句將每個索引作為引數傳遞給方法,可以檢索給定字元陣列中每個元素的程式碼點。

package com.tutorialspoint;

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

      // create a char array c and assign values
      char[] c_arr = new char[] { 'a', 'b', 'c', 'd', 'e' };

      // create 2 int's res, index and assign value to index
      int res, index;

      // using a for loop assign the codePointAt on array c at index to res
      for (index = 0; index < c_arr.length; index++){
        res = Character.codePointAt(c_arr, index);
        String str = "Unicode code point is " + res;
        
        // print res value
        System.out.println( str );
      }
   }
}

如果您編譯上面的程式碼,則輸出將獲得如下:

Unicode code point is 97
Unicode code point is 98
Unicode code point is 99
Unicode code point is 100
Unicode code point is 101

使用限制獲取字元陣列中給定索引處的字元的程式碼點示例

我們可以透過傳遞額外的引數“limit”來限制此字元陣列中的搜尋。在這種情況下,該方法將僅執行到給定限制的搜尋並返回程式碼點。

package com.tutorialspoint;

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

      // create a char array c and assign values
      char[] c = new char[] { 'a', 'b', 'c', 'd', 'e' };

      // create and assign value to integers index, limit
      int index  = 1, limit = 3;

      // create an int res
      int res;

      // assign result of codePointAt on array c at index to res using limit
      res = Character.codePointAt(c, index, limit);
      String str = "Unicode code point is " + res;

      // print res value
      System.out.println( str );
   }
}

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

Unicode code point is 98

使用無效限制在獲取字元陣列中給定索引處的字元的程式碼點時遇到異常示例

但是,如果 index 引數大於 array 引數的長度或 limit 引數,則該方法會丟擲 IndexOutOfBounds 異常。

package com.tutorialspoint;

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

      // create a char array c and assign values
      char[] c = new char[] {'a', 'b', 'c', 'd', 'e'};

      // assign an index value greater than array length
      int res, index = 6;

      // assign result of codePointAt on array c at index to res
      res = Character.codePointAt(c, index);

      String str = "Unicode code point is " + res;

      // print res value
      System.out.println( str );
   }
}

異常

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6at java.lang.Character.codePointAtImpl(Character.java:4952)
at java.lang.Character.codePointAt(Character.java:4915)
	at com.tutorialspoint.CharacterDemo.main(CharacterDemo.java:16)

獲取CharSequence中給定索引處的字元的程式碼點示例

當我們將 CharSequence 和索引值作為引數傳遞給 codePointAt(CharSequence seq, int index) 方法時,返回值是給定 CharSequence 中索引處存在的字元。

package com.tutorialspoint;

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

      // create a CharSequence seq and assign value
      CharSequence seq = "Hello";

      // create and assign value to index
      int index  = 4;

      // create an int res
      int res;

      // assign result of codePointAt on seq at index to res
      res = Character.codePointAt(seq, index);
      String str = "Unicode code point is " + res;

      // print res value
      System.out.println( str );
   }
}

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

Unicode code point is 111

在獲取 null CharSequence 中給定索引處的字元的程式碼點時遇到異常示例

但是,如果 CharSequence 初始化為 null 值並作為引數傳遞給方法,則會引發 NullPointer 異常。

package com.tutorialspoint;

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

      // create a CharSequence seq and assign value
      CharSequence seq = null;

      // create and assign value to index
      int index  = 5;

      // create an int res
      int res;

      // assign result of codePointAt on seq at index to res
      res = Character.codePointAt(seq, index);

      // print res value
      System.out.println("Unicode code point is " + res);
   }
}

異常

Exception in thread "main" java.lang.NullPointerExceptionat java.lang.Character.codePointAt(Character.java:4883)
java_lang_character.htm
廣告

© . All rights reserved.