w3resource logo

Introduction to Java Programming

Java String: codePointBefore() Method

Secondary Nav

public int codePointBefore(int index)

The codePointBefore() method is used to get a character (Unicode code point) before the specified index. The index refers to char values (Unicode code units) and ranges from 1 to length.

If the char value at (index - 1) is in the low-surrogate range, (index - 2) is not negative, and the char value at (index - 2) is in the high-surrogate range, then the supplementary code point value of the surrogate pair is returned. If the char value at index - 1 is an unpaired low-surrogate or a high-surrogate, the surrogate value is returned.

Java Platform: Java SE 8

Syntax :

codePointBefore(int index)


Name Description Type
index the index following the code point that should be returned int

Return Value :
The Unicode code point value before the given index.

Return Value Type: int

Throws: IndexOutOfBoundsException - if the index argument is less than 1 or greater than the length of this string.

Example : Java String codePointBefore() Method

The following example shows the usage of java String() method.

public class Example {

public static void main(String[] args) {
    String str = "w3resource.com";
System.out.println("Original String : " + str);

    // codepoint before index 1
int val1 = str.codePointBefore(1);

   // codepoint before index 9
int val2 = str.codePointBefore(9);

    // prints character before index1 in string
System.out.println("Character(unicode point) = " + val1);
    // prints character before index9 in string
System.out.println("Character(unicode point) = " + val2);


Original String : w3resource.com                       
Character(unicode point) = 119                         
Character(unicode point) = 99 

Example of Throws: codePointBefore(int index) Method

IndexOutOfBoundsException - if the index argument is less than 1 or greater than the length of this string.


int val2 = str.codePointBefore(-9);

in the above example.


Original String : w3resource.com                       
Exception in thread "main" java.lang.StringIndexOutOfBo
undsException: String index out of range: -9           
        at java.lang.String.codePointBefore(String.java
        at Exercise.main(Example.java:12)

Java Code Editor:

Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program