In this section, we will learn what the String codePointBefore() method is and how to use it in Java.
What is Java String codePointBefore() Method?
The String codePointBefore() method is used to get the Unicode number of the character that is before the index number we set as the argument of this method.
Java codePointBefore() Method Syntax:
public int codePointBefore(int index)
codePointBefore() Method Parameters:
The codePointBefore() method takes an integer number as its argument and it will return the Unicode of the character that is before this index! For example, if we put the value 1, we will get the Unicode number of the character that is in the index 0.
codePointBefore() Method Return Value:
The return value of the method is an integer value that contains the Unicode number of the target character.
codePointBefore() Method Exceptions:
The lowest value that we can set for this method is 1. This is because the index number of the first element in string is 0 and if we put the value 0 as the argument of this method, it will try to get the Unicode in the index -1! But there’s no such index number in a String. For this reason, if we use the value 0 we will get a runtime exception which is of type StringIndexOutOfBoundsException class.
Example: using String codePointBefore() method
public class Simple { public static void main(String[] args) { String s = "Hello"; int unicode = s.codePointBefore(1); System.out.println(unicode); } }
Output: 72