codePointBefore() - String Methods in Java


This article is a part of String Methods in Java.
This method returns the Unicode value of the character before the specified index. Let me tell you if you don`t know already, that the index of the first character of a string is 0. The parameter is of int type, representing the index of the character following the Unicode that should be returned.


Return type

The return type of the codePointBefore() method is int. Let us look at an example code to understand it better.

Example


Output

Let us look at the output of the above example code.

String Methods codePointBefore()
Fig.1- String Method - codePointBefore()

Here the index = 1 points to the character “e,” the codePointBefore() method then points to the character preceding the indexed character which is “L,” and displays its Unicode value, i.e 76.

IndexOutOfBound exception in codePointBefore()

When we pass negative index or an index greater than length() - 1 then the codePointBefore() method throws IndexOutOfBound exception. Let us look at the code for this case.


Output

Let us look at the output of the above example code.

String Methods codePointBefore() IndexOutOfBound
Fig.2- String Method - codePointBefore() IndexOutOfBound

Since, the index is a negative number the method throws an exception.

Read More

Author : Satyam Kumar -



You will also like: