⬅ Previous Topic
Java StringcodePointBefore()
methodNext Topic ⮕
Java StringcompareTo()
methodcodePointCount()
method⬅ Previous Topic
Java StringcodePointBefore()
methodNext Topic ⮕
Java StringcompareTo()
methodThe 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.
public int codePointCount(int beginIndex,
int endIndex)
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). |
This method returns an int
representing the total number of Unicode code points within the specified range.
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.
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.
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'.
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.
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.
⬅ Previous Topic
Java StringcodePointBefore()
methodNext Topic ⮕
Java StringcompareTo()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.