Java Character codePointBefore(charSequence seq, int index) Method

Java charPointBefore() method is a part of Character Class. This method returns the codePoint preceding the specified index of the CharacterSequence. It must be noted that if the value of the char in the CharacterSequence at (index-1) is in the low surrogate range, (index-2) is not negative and If the value of char in the CharacterSequence at (index-2) is in the high surrogate range, the supplementary code point to this surrogate pair is returned. Otherwise, the char value at (index-1) is returned.

Syntax:

public static int codePointBefore(charSequence seq, int index) 

Parameters:

The parameters passed are

  • charSequence array

  • int index before which codePoint of the char value in the character sequence is to be returned.

Returns:

Returns the codePoint of the char value in the charSequence before the specified index.

Example 1:

Here, the codepoint value of the character in the character sequence before the specified index is returned.

import java.lang.Character;

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        CharSequence s = "Welcome to StudyTonight";  
        System.out.println("Input charSequence is : " +s); 
        int index = 4;  
        System.out.println(Character.codePointBefore(s,index));  
    }  
}


Input charSequence is : Welcome to StudyTonight
99

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 charSequence");
          Scanner sc = new Scanner(System.in);
          CharSequence s = sc.nextLine();
          System.out.println("Enter index");
          int n = sc.nextInt();
          System.out.println("The codepoint value is : " +Character.codePointBefore(s,n));      
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input");
        }
    }  
}


Enter charSequence
god is great
Enter index
2
The codepoint value is : 111
**********************************
Enter charSequence
mohit
Enter index
4
The codepoint value is : 105

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.