Java - Character codePointBefore() 方法



描述

Java 的Character codePointBefore() 方法將檢索字元陣列中某個字元索引之前字元的碼點。這些碼點可以表示普通字元和補充字元。

當字元陣列中給定索引之前的數值滿足以下條件時,將獲得補充碼點字元:

  • 如果 (index-2) 位置的 char 值屬於高代理範圍 (U+D800 到 U+DBFF) 且非負。

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

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

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

語法

以下是使用不同和擴充套件引數的 Java Character codePointBefore() 方法的各種語法。

public static int codePointBefore(char[] a, int index)
(or)
public static int codePointBefore(CharSequence seq, int index)
(or)
public static int codePointBefore(char[] a, int index, int start)

引數

  • a - char 陣列

  • index - 應該返回的碼點之後的索引

  • start - char 陣列中第一個陣列元素的索引。

  • seq - CharSequence 例項

返回值

此方法返回給定索引之前的 Unicode 碼點值。

在給定索引的 char 陣列中獲取前導字元的碼點示例

以下示例演示了 Java Character codePointBefore() 方法的用法。當我們採用簡單的字元陣列和指定的小於陣列長度的索引作為方法的引數時,返回值將是該索引處前導字元的碼點。

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 index
      int index  = 2;

      // create an int res
      int res;

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

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

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

輸出

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

Unicode code point is 98

在給定索引的 CharSequence 中獲取前導字元的碼點示例

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

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      CharSequence seq = "Hello";
      int index  = 4;
      int res;

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

輸出

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

Unicode code point is 108

示例

以下示例演示了 Java Character codePointBefore(char[] a, int index, int start) 方法的用法。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      char[] c = new char[] { 'A', 'b', 'C', 'd'};
      int index  = 3, start = 1;
      int res;
      
      // assign result of codePointBefore on c array at index to res using start
      res = Character.codePointBefore(c, index, start);
      String str = "Unicode code point is " + res;
      System.out.println( str );
   }
}

輸出

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

Unicode code point is 67

在 char 陣列中使用無效索引獲取前導字元的碼點時遇到異常的示例

如果將無效索引作為引數傳遞給方法,則會引發 ArrayIndexOutOfBoundsException 異常。

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 a negative value to index to throw an ArrayIndexOutOfBoundsException exception
      int index  = -1;

      // create an int res
      int res;

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

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

異常

在編譯和執行上述程式時,該方法會丟擲如下異常:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
at java.lang.Character.codePointBeforeImpl(Character.java:5055)
	at java.lang.Character.codePointBefore(Character.java:5016)
at com.tutorialspoint.CharacterDemo.main(CharacterDemo.java:19)

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

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

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      CharSequence seq = null;
      int index  = 9;
      int output = Character.codePointBefore(seq, index);
      System.out.println(output);
   }
}

異常

在編譯和執行上述程式時,該方法會丟擲如下異常:

Exception in thread "main" java.lang.NullPointerException
at java.lang.Character.codePointBefore(Character.java:4984)at com.tutorialspoint.CharacterDemo.main(CharacterDemo.java:10)
java_lang_character.htm
廣告
© . All rights reserved.