Java - Character codePointBefore() Method



The Java Character codePointBefore() will retrieve the code point of a character preceding an index of a certain char array. These code points can represent ordinary character and supplementary characters as well.

Supplementary code point characters will be obtained when the value preceding the given index in a char array satisfies the given conditions below −

  • If the char value at (index-2) belongs to the high-surrogate range (U+D800 to U+DBFF) and not negative.

  • If the value at the preceding index belongs to the low surrogate range (U+DC00 to U+DFFF).

Ordinary code point characters will be obtained in any other case.

Note − This method occurs in several polymorphic forms with different parameters.

Syntax

Following is the various syntaxes for Java Character codePointBefore() method using different and extended parameters.

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)

Parameters

  • a − The char array

  • index − The index following the code point that should be returned

  • start − The index of the first array element in the char array.

  • seq − The CharSequence instance

Return Value

This method returns the Unicode code point value before the given index.

Example

The following example shows the usage of Java Character codePointBefore(char[] a, int index) method.

package com.tutorialspoint;
import java.lang.*;
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 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Unicode code point is 98

Example

The following example shows the usage of Java Character codePointBefore(CharSequence seq, int index) method.

package com.tutorialspoint;
import java.lang.*;
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 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Unicode code point is 108

Example

The following example shows the usage of Java Character codePointBefore(char[] a, int index, int start) method.

package com.tutorialspoint;
import java.lang.*;
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 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Unicode code point is 67

Example

However, there are scenarios where an IndexOutOfBounds Exception is thrown by this method; when either the index or start parameters exceed the length of the character array or sequence, or when the index is set to be negative.

package com.tutorialspoint;
import java.lang.*;
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 indexoutofbounds 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

On compiling and executing the program above, the method throws an exception as follows −

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)

Example

This method also throws a NullPointer Exception if the CharSequence is passed as an empty/null string.

package com.tutorialspoint;
import java.lang.*;
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

On compiling and executing the program above, the method throws an exception as follows −

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
Advertisements