Java Character codePointBefore(char[] a, int index, int start)Method

Java codePointAt() method is a part of the Character class. This method returns the codepoint of the char value in the char array preceding the specified index where only those array elements can be used which have the index greater than or equal to start. It must be noted that if the value of the char in the char array at (index-1) is in the low surrogate range, (index-2) is not less than start and if the value of char in the char array at (index-2) is in the high surrogate range, the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at (index-1) is returned.

Syntax:

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

Parameters:

The parameters passed are:

  • char array

  • int index according to which the preceding codePoint value is to be returned

  • int start which is the index of a first array element in the char array.

Returns:

Returns the index of the first array element in the char array.

Example 1:

Here, the codepoint value of the character preceding the specified index is returned.

public class StudyTonight
{  
  public static void main(String args[])
  {  
    char[] ch1 = new char[] { 'm', 'o', 'h', 'i', 't' };  
    System.out.println("The input is : Mohit");  
    int i1  = 2, s1 = 1;  
    int r1 = Character.codePointBefore(ch1, i1, s1);  
    System.out.println("Unicode code point is : " + r1 );  
    char[] ch2 = new char[] { 's', 't', 'u', 'd', 'y','t', 'o', 'n','i','g','h','t' };  
    System.out.println("The input is : StudyTonight");  
    int i2  = 2, s2 = 1;  
    int r2 = Character.codePointBefore(ch2, i2, s2);  
    System.out.println("Unicode code point is : " + r2 ); 
  }
}


The input is : Mohit
Unicode code point is : 111
The input is : StudyTonight
Unicode code point is : 116

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the desired output.

import java.lang.Character;
import java.util.Scanner;

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        try
        {
          System.out.println("Enter char array");
          Scanner sc = new Scanner(System.in);
          char []ch = sc.nextLine().toCharArray();
          System.out.println("Enter index");
          int n = sc.nextInt();
          System.out.println("Enter start");
          int s = sc.nextInt();
          System.out.println("The codepoint value is : " +Character.codePointBefore(ch,n,s));  
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input");
        }
    }  
}


Enter char array
corporation
Enter index
3
Enter start
2
The codepoint value is : 114
************************************
Enter char array
dellpc
Enter index
2
Enter start
1
The codepoint value is : 101

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.