Java StringBuilder.codePointBefore() – Examples

In this tutorial, we will learn about the Java StringBuilder.codePointBefore() function, and learn how to use this function to get the code point present before the specified index, with the help of examples.

codePointBefore(int index)

StringBuilder.codePointBefore() returns the character (Unicode code point) value as integer, before the specified index.

If an index of 5 is specified, then the code point at index 4 is returned.

Syntax

The syntax of codePointBefore() function is

codePointBefore(int index)

where

ParameterDescription
indexAn integer representing the position in StringBuilder.

Returns

The function returns an integer.

Example 1 – codePointBefore(int index)

In this example, we will create a StringBuilder object with some initial string literal. We will call codePointBefore() on this object with an index value of 2. So, the code point at index 1 should be returned.

Java Program

public class Example { 
    public static void main(String[] args) { 
        // create a StringBuilder object 
        StringBuilder stringBuilder = new StringBuilder("abcdefgh"); 

        int index = 2;
        int codePoint = stringBuilder.codePointBefore(index);
        System.out.println("Code Point before index " + index + " is : " + codePoint);   
    } 
}

Output

Code Point before index 2 is : 98

Example 2 – codePointBefore(0)

In this example, we will create a StringBuilder object with some initial string literal. We will call codePointBefore() on this object with an index value of 0. Since, the index before zero would be -1, codePointBefore() should throw ava.lang.StringIndexOutOfBoundsException.

The result would be same, if you try to access code point whose index is out of bounds for the StringBuilder.

Java Program

public class Example { 
    public static void main(String[] args) { 
        // create a StringBuilder object 
        StringBuilder stringBuilder = new StringBuilder("abcdefgh"); 

        int index = 0;
        int codePoint = stringBuilder.codePointBefore(index);
        System.out.println("Code Point before index " + index + " is : " + codePoint);   
    } 
}

Output

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
	at java.base/java.lang.AbstractStringBuilder.codePointBefore(Unknown Source)
	at java.base/java.lang.StringBuilder.codePointBefore(Unknown Source)
	at Example.main(Example.java:7)

Example 3 – codePointBefore(length)

In this example, we will call codePointBefore() on StringBuilder object with this sequence length as index. The method should return the last element in the StringBuilder.

Java Program

public class Example { 
    public static void main(String[] args) { 
        // create a StringBuilder object 
        StringBuilder stringBuilder = new StringBuilder("abcdefgh"); 

        int index = stringBuilder.length();
        int codePoint = stringBuilder.codePointBefore(index);
        System.out.println("Code Point before index " + index + " is : " + codePoint);   
    } 
}

Output

Code Point before index 8 is : 104

Conclusion

In this Java Tutorial, we have learnt the syntax of Java StringBuilder.codePointBefore() function, and also learnt how to use this function with the help of examples.