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.
Syntax:
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:
Example 1:
class GFG {
public static void main(String[] args)
{
StringBuilder str = new StringBuilder();
str.append( "Geek" );
int unicode = str.codePointAt( 1 );
System.out.println( "StringBuilder Object"
+ " contains = " + str);
System.out.println( "Unicode of Character"
+ " at Position 1 "
+ "in StringBuilder = "
+ unicode);
unicode = str.codePointAt( 3 );
System.out.println( "Unicode of Character "
+ "at Position 3 "
+ "in StringBuilder = "
+ unicode);
}
}
|
Output:
StringBuilder Object contains = Geek
Unicode of Character at Position 1 in StringBuilder = 101
Unicode of Character at Position 3 in StringBuilder = 107
Example 2:
class GFG {
public static void main(String[] args)
{
StringBuilder
str
= new StringBuilder( "WelcomeGeeks" );
System.out.println( "String is " + str.toString());
for ( int i = 0 ; i < str.length(); i++) {
char ch = str.charAt(i);
int unicode = str.codePointAt(i);
System.out.println( "Unicode of Char " + ch
+ " at position " + i
+ " is " + unicode);
}
}
}
|
Output:
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
class GFG {
public static void main(String[] args)
{
StringBuilder
str
= new StringBuilder( "WelcomeGeeks" );
try {
int i = str.codePointAt(str.length());
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.StringIndexOutOfBoundsException:
String index out of range: 12
Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#codePointAt(int)
Last Updated :
15 Oct, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...