Java StringBuilder.codePointAt() – Examples
In this tutorial, we will learn about the Java StringBuilder.codePointAt() function, and learn how to use this function to get Unicode code point at specific index, with the help of examples.
codePointAt(int index)
StringBuilder.codePointAt() returns the character (Unicode code point) at the specified index.
The index has to be in the bounds of the StringBuilder length, else, you codePointAt() throws StringIndexOutOfBoundsException.
Syntax
The syntax of codePointAt() function is
codePointAt(int index)
where
Parameter | Description |
---|---|
index | An integer representing the position of code point in the StringBuilder. index starts at 0. |
Returns
The function returns, an integer, code point value at the specified index.
Example 1 – codePointAt()
In this example, we will create a StringBuilder object with some initial string literal. We will use codePointAt() to get Unicode code point at indexes 1 and 3, and print them.
Java Program
public class Example {
public static void main(String[] args) {
// create a StringBuilder object
StringBuilder stringBuilder = new StringBuilder("abcdefgh");
// get Unicode code point at index 1
int codePoint = stringBuilder.codePointAt(1);
System.out.println("Code Point at Index 1 is : "+ codePoint);
// get Unicode of char at position 3
codePoint = stringBuilder.codePointAt(3);
System.out.println("Code Point at Index 3 is : "+ codePoint);
}
}
Output
Code Point at Index 1 is : 98
Code Point at Index 3 is : 100
Example 2 – codePointAt() – Print All Code Points
In this example, we will print all the Unicode code points, by iterating over the sequence of StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
// create a StringBuilder object
StringBuilder stringBuilder = new StringBuilder("abcdefgh");
for(int i = 0; i < stringBuilder.length(); i++) {
System.out.println("Code Point at Index "+ i +" is : "+ stringBuilder.codePointAt(i));
}
}
}
Output
Code Point at Index 0 is : 97
Code Point at Index 1 is : 98
Code Point at Index 2 is : 99
Code Point at Index 3 is : 100
Code Point at Index 4 is : 101
Code Point at Index 5 is : 102
Code Point at Index 6 is : 103
Code Point at Index 7 is : 104
Example 3 – codePointAt(int index) – StringIndexOutOfBoundsException
In this example, we will try to access Unicode code point at an index that is out of range for the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
// create a StringBuilder object
StringBuilder stringBuilder = new StringBuilder("abcdefgh");
int index = 25;
System.out.println(stringBuilder.codePointAt(index));
}
}
Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: index 25,length 8
at java.base/java.lang.String.checkIndex(Unknown Source)
at java.base/java.lang.AbstractStringBuilder.codePointAt(Unknown Source)
at java.base/java.lang.StringBuilder.codePointAt(Unknown Source)
at Example.main(Example.java:7)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java StringBuilder.codePointAt() function, and also learnt how to use this function with the help of examples.