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:
class GFG {
public static void main(String[] args)
{
StringBuffer str = new StringBuffer();
str.append( "Geeksforgeeks" );
int unicode = str.codePointAt( 10 );
System.out.println( "Unicode of Character "
+ "at Position 10 "
+ "in StringBuffer = "
+ unicode);
}
}
|
Output:
Unicode of Character at Position 10 in StringBuffer = 101
Example 2: To demonstrate IndexOutOfBoundsException
class GFG {
public static void main(String[] args)
{
StringBuffer
str
= new StringBuffer( "GeeksForGeeks Contribute" );
try {
int i = str.codePointAt( 25 );
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
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)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Dec, 2018
Like Article
Save Article