What is the String.codePointBefore() method in Java?
The codePointBefore()method in Java is used to get the Unicode value of a character in the string before the specified index.
Syntax
The codePointBefore() method can be declared as shown in the code snippet below:
public int codePointBefore(int ind)
Parameter
ind: The index following the character whose Unicode value is required. The index value must be between1and the length of the string.
Return value
The codePointBefore() method returns an int, which is the Unicode value of the specified character.
The codePointBefore() method throws the IndexOutOfBoundsException if:
- The index
indis less than one. - The index
indis greater than the length of the string.
Code
Consider the code snippet below, which demonstrates the use of the codePointBefore() method.
class Main {public static void main(String[] args){String str = "Hello world!";int code = str.codePointBefore(4);System.out.println("Code point at 3rd index: " + code);}}
Explanation
A string str is declared in line 5. The codePointBefore() method is used in line 6 to get the Unicode value of the character at third index in str.