StringBuilder codePointAt() in Java with Examples
The codePointAt(int index) method of StringBuilder class takes an index as a parameter and returns a character unicode point at that index in String contained by StringBuilder or we can say charPointAt() method returns the “unicode number” of the character at that index. The index refers to char values (Unicode code units) and the value of index must be lie between 0 to length-1.
If the char value present at the given index lies in the high-surrogate range, the following index is less than the length of this sequence, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public int codePointAt(int index)
Parameters: This method accepts one int type parameter index which represents index of the character whose unicode value to be returned.
Return Value: This method returns “unicode number” of the character at the specified position.
Exception: This method throws IndexOutOfBoundsException when index is negative or greater than or equal to length().
Below programs demonstrate the codePointAt() method of StringBuilder Class:
StringBuilder Object contains = Geek Unicode of Character at Position 1 in StringBuilder = 101 Unicode of Character at Position 3 in StringBuilder = 107
String is WelcomeGeeks Unicode of Char W at position 0 is 87 Unicode of Char e at position 1 is 101 Unicode of Char l at position 2 is 108 Unicode of Char c at position 3 is 99 Unicode of Char o at position 4 is 111 Unicode of Char m at position 5 is 109 Unicode of Char e at position 6 is 101 Unicode of Char G at position 7 is 71 Unicode of Char e at position 8 is 101 Unicode of Char e at position 9 is 101 Unicode of Char k at position 10 is 107 Unicode of Char s at position 11 is 115
Example 3: To demonstrate IndexOutOfBoundsException
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 12