Introduction
The codePointCount()
method in Java's String
class provides a way to count the number of Unicode code points within a specified range of characters. Unlike methods that operate on individual bytes, this is crucial for handling strings containing supplementary characters (characters outside the Basic Multilingual Plane - BMP), which require more than one byte to represent.
Syntax
public int codePointCount(int beginIndex,
int endIndex)
Parameters
Parameter | Description |
---|---|
beginIndex |
The index of the first character to be included in the count. |
endIndex |
The index after the last character to be included in the count (exclusive). |
Return Value
This method returns an int
representing the total number of Unicode code points within the specified range.
Examples
Example 1: Counting Code Points in a Simple String
Let's start with a simple example. This demonstrates how to count code points in a basic string where each character is represented by a single byte.
String str = "Hello";
int count = str.codePointCount(0, str.length());
System.out.println("Number of code points: " + count);
Number of code points: 5
In this case, each character ('H', 'e', 'l', 'l', 'o') is a single code point. Therefore, the method returns 5.
Example 2: Handling Supplementary Characters
This example showcases how to count code points when dealing with characters that require more than one byte (supplementary characters). The Unicode character U+1F600 (Grinning Face) is a supplementary character.
String str = "Hello😀"; // Includes a grinning face emoji.
int count = str.codePointCount(0, str.length());
System.out.println("Number of code points: " + count);
Number of code points: 6
Even though the string has only six characters when displayed, the grinning face emoji requires two code units. The codePointCount()
method correctly counts it as a single code point.
Example 3: Counting Code Points within a Substring
This example demonstrates how to count code points within a specific portion of the string, using begin and end indices.
String str = "Hello😀World";
int count = str.codePointCount(6, 12);
System.out.println("Number of code points: " + count);
Number of code points: 2
Here, we are counting the code points from index 6 (inclusive) to index 12 (exclusive). This includes the grinning face emoji and the character 'W'.
Example 4: Using Negative Indices
It's important to note that using negative indices with this method will result in an IndexOutOfBoundsException
. Attempting to use a negative index is not valid.
String str = "Hello";
try {
int count = str.codePointCount(-1, 2);
System.out.println("Number of code points: " + count);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: IndexOutOfBoundsException - Negative index used.");
}
Error: IndexOutOfBoundsException - Negative index used.
This illustrates the importance of validating your input indices to prevent runtime errors.
Example 5: Empty String
Let's look at what happens when we use an empty string.
String str = "";
int count = str.codePointCount(0, 0);
System.out.println("Number of code points: " + count);
Number of code points: 0
As expected, an empty string contains no characters, so the method returns 0.