The StringBuilder.codePointAt()
method in Java is used to retrieve the Unicode code point value of the character at a specified index within a StringBuilder
object.
Table of Contents
- Introduction
codePointAt
Method Syntax- Examples
- Retrieving Code Point by Index
- Handling IndexOutOfBoundsException
- Using Code Points for Supplementary Characters
- Real-World Use Case
- Conclusion
Introduction
The StringBuilder.codePointAt()
method is a member of the StringBuilder
class in Java. It allows you to access the Unicode code point value of the character at a specified index within the StringBuilder
object. This method is particularly useful when dealing with Unicode characters, including supplementary characters that require more than one char
value.
codePointAt() Method Syntax
The syntax for the codePointAt
method is as follows:
public int codePointAt(int index)
- index: The position of the character whose Unicode code point value is to be retrieved. The index is zero-based, meaning the first character of the sequence is at index 0.
Examples
Retrieving Code Point by Index
The codePointAt
method can be used to access the Unicode code point value of characters at specific positions within a StringBuilder
.
Example
public class StringBuilderCodePointAtExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World!");
// Get the code points at specific indices
int codePointFirst = sb.codePointAt(0);
int codePointSeventh = sb.codePointAt(6);
int codePointLast = sb.codePointAt(sb.length() - 1);
// Print the code points
System.out.println("Code point of first character: " + codePointFirst);
System.out.println("Code point of seventh character: " + codePointSeventh);
System.out.println("Code point of last character: " + codePointLast);
}
}
Output:
Code point of first character: 72
Code point of seventh character: 44
Code point of last character: 33
Handling IndexOutOfBoundsException
Attempting to access an index that is out of bounds will result in an IndexOutOfBoundsException
. It’s important to ensure that the specified index is within the valid range.
Example
public class StringBuilderCodePointAtExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World!");
try {
// Attempt to access an invalid index
int invalidCodePoint = sb.codePointAt(20);
System.out.println("Code point at index 20: " + invalidCodePoint);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: String index out of range: 20
Using Code Points for Supplementary Characters
For supplementary characters (characters outside the Basic Multilingual Plane, BMP), which are represented by a pair of char
values (a surrogate pair), the codePointAt
method can be used to get the correct code point value.
Example
public class StringBuilderCodePointAtExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World! \uD83D\uDE00"); // Unicode for ? (grinning face)
// Get the code point of the supplementary character (emoji)
int codePointEmoji = sb.codePointAt(13);
System.out.println("Code point of emoji: " + codePointEmoji);
}
}
Output:
Code point of emoji: 128512
Real-World Use Case
Example: Counting Non-BMP Characters
In a real-world scenario, you might need to count the number of supplementary (non-BMP) characters in a StringBuilder
. Using the codePointAt
method, you can iterate through the characters and count those with code points outside the BMP.
Example Code
public class NonBMPCharacterCounter {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World! \uD83D\uDE00 \uD83C\uDF0D"); // ? (grinning face), ? (earth globe)
int nonBMPCount = countNonBMPCharacters(sb);
System.out.println("Number of non-BMP characters: " + nonBMPCount);
}
public static int countNonBMPCharacters(StringBuilder sb) {
int count = 0;
for (int i = 0; i < sb.length(); i++) {
int codePoint = sb.codePointAt(i);
if (Character.isSupplementaryCodePoint(codePoint)) {
count++;
i++; // Skip the next char as it is part of the surrogate pair
}
}
return count;
}
}
Output:
Number of non-BMP characters: 2
Conclusion
The StringBuilder.codePointAt()
method in Java is used for retrieving the Unicode code point value of characters within a StringBuilder
object. By understanding how to use this method, you can efficiently handle and manipulate Unicode characters, including supplementary characters. Whether you need to retrieve code points by index, handle potential exceptions, or work with characters beyond the BMP, the codePointAt
method provides a reliable solution for these tasks.