The codePointAt() method of StringBuffer class returns a character Unicode point at that index in sequence contained by StringBuffer. This method returns the “Unicodenumber” of the character at that index. 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.
Syntax:
public int codePointAt(int index)
Parameters: This method takes one parameter index which is int value representing index of the character whose unicode value to be returned.
Return Value: This method returns unicode number of the character at the specified index.
Exception: This method throws IndexOutOfBoundsException when index is negative or greater than or equal to length().
Below programs demonstrate the codePointAt() method of StringBuffer Class:
Example 1:
// Java program to demonstrate // the codePointAt() method class GFG { public static void main(String[] args) { // create a StringBuffer object StringBuffer str = new StringBuffer(); // add the String to StringBuffer Object str.append("Geeksforgeeks"); // get unicode of char at position 10 int unicode = str.codePointAt(10); // print the result System.out.println("Unicode of Character " + "at Position 10 " + "in StringBuffer = " + unicode); } } |
Unicode of Character at Position 10 in StringBuffer = 101
Example 2: To demonstrate IndexOutOfBoundsException
// Java program demonstrate // IndexOutOfBoundsException thrown by // the codePointAt() Method. class GFG { public static void main(String[] args) { // create a StringBuffer object // with a String pass as parameter StringBuffer str = new StringBuffer("GeeksForGeeks Contribute"); try { // get char at position 25 which is // greater then length int i = str.codePointAt(25); } catch (IndexOutOfBoundsException e) { System.out.println("Exception: " + e); } } } |
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 25
References:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#codePointAt(int)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- StringBuilder codePointAt() in Java with Examples
- StringBuffer deleteCharAt() Method in Java with Examples
- StringBuffer appendCodePoint() Method in Java with Examples
- StringBuffer delete() Method in Java with Examples
- StringBuffer replace() Method in Java with Examples
- StringBuffer reverse() Method in Java with Examples
- StringBuffer append() Method in Java with Examples
- StringBuffer codePointCount() method in Java with Examples
- StringBuffer codePointBefore() method in Java with Examples
- StringBuffer trimToSize() method in Java with Examples
- StringBuffer toString() method in Java with Examples
- StringBuffer ensureCapacity() method in Java with Examples
- StringBuffer offsetByCodePoints() method in Java with Examples
- StringBuffer setCharAt() method in Java with Examples
- StringBuffer getChars() method in Java with Examples
- StringBuffer indexOf() method in Java with Examples
- StringBuffer substring() method in Java with Examples
- StringBuffer lastIndexOf() method in Java with Examples
- Matcher appendTail(StringBuffer) method in Java with Examples
- Matcher appendReplacement(StringBuffer, String) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

