LAST UPDATED: OCTOBER 16, 2020
Java codePointBefore(char[] a, int index) method
Java charPointBefore()
method is a part of Character
class. This method returns the codePoint preceding the specified index of the char array. 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 negative and if the value of char in the char array 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(char[] a, int index)
Parameters:
The parameters passed is the char
array and int
index before which codePoint of the char value in the char array is to be returned.
Returns:
Returns the codePoint of the char
value in the char value before the specified index.
Example 1:
Here, the codepoint value of the character before the specified index is returned.
import java.lang.Character;
public class StudyTonight
{
public static void main(String[] args)
{
char[] ch = new char[] { 'm', 'o', 'h', 'i', 't' };
int index = 4;
int r = Character.codePointBefore(ch,index);
System.out.println("The codepoint value is : "+r);
}
}
The codepoint value is:105
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 the characters");
Scanner sc = new Scanner(System.in);
char[] ch = sc.next().toCharArray();
System.out.println("Enter index");
int n = sc.nextInt();
System.out.println("The Codepoint value is : "+Character.codePointBefore(ch,n));
}
catch(Exception e)
{
System.out.println("Invalid Input");
}
}
}
Enter the characters
studytonight
Enter index
3
The codepoint value is : 117
******************
Enter the characters
god is great
Enter index
6
The codepoint value is : 115
****************
Enter the characters
0x667
Enter index
7
Invalid Input
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.