STRING INSTANCE METHODS
Java String codePointCount() method
codePointCount() counts Unicode code points in the specified String range. It matters when text contains emoji or other characters that occupy more than one char.
Counting code points in the whole string
String tekstas = "Java";
int kiekis = tekstas.codePointCount(0, tekstas.length());
System.out.println(kiekis);4
- The variable
tekstasstores the value Java. 0is the start of the range.tekstas.length()is the position after the lastcharunit.codePointCount(0, tekstas.length())counts all the string's code points.
Why is codePointCount() an instance method?
codePointCount() is called on a specific string because it counts that string's Unicode code points:
int kiekis = tekstas.codePointCount(0, tekstas.length());
We therefore do not write String.codePointCount(...). The method needs a specific String object and the range boundaries.
Method syntax
public int codePointCount(int beginIndex, int endIndex)
| Part | Meaning |
|---|---|
| Calling object | tekstas in the line tekstas.codePointCount(0, tekstas.length()) |
| First parameter | beginIndex – the inclusive start of the range |
| Second parameter | endIndex – the exclusive end of the range |
| Return type | int |
| Valid bounds | 0 <= beginIndex <= endIndex <= length() |
| Error | IndexOutOfBoundsExceptionwhen the bounds are outside the string or the start exceeds the end |
Counting within a range
String tekstas = "Programavimas";
System.out.println(tekstas.codePointCount(0, 7));
System.out.println(tekstas.codePointCount(7, tekstas.length()));7
6
beginIndex is included in the range, while endIndex is excluded. Therefore, codePointCount(0, 7) counts positions from 0 to 6, while codePointCount(7, tekstas.length()) counts from 7 to the end of the string.
The difference from length()
String tekstas = "A😀B";
System.out.println(tekstas.length());
System.out.println(tekstas.codePointCount(0, tekstas.length()));4
3
length() returns the number of UTF-16 char units. codePointCount() returns the Unicode code point count. The text A😀B has 4 UTF-16 units but only 3 code points: A, 😀 and B.
An emoji as one code point
String tekstas = "A😀B";
System.out.println(tekstas.codePointCount(0, 1));
System.out.println(tekstas.codePointCount(1, 3));
System.out.println(tekstas.codePointCount(3, 4));1
1
1
The emoji 😀 occupies the Java string index range from 1 to 3because it consists of two UTF-16 units. However, codePointCount(1, 3) counts it as one Unicode code point.
An empty range returns 0
String tekstas = "Java";
System.out.println(tekstas.codePointCount(2, 2));0
When beginIndex and endIndex are equal, the range contains no positions. This is valid if both bounds lie between 0 and length().
The start cannot be negative
String tekstas = "Java";
System.out.println(tekstas.codePointCount(-1, 2));IndexOutOfBoundsException
beginIndex must be at least 0. A negative index is not a valid Java String position.
The end cannot exceed length()
String tekstas = "Java";
System.out.println(tekstas.codePointCount(1, 5));IndexOutOfBoundsException
Java has length 4, so the maximum valid endIndex value is 4. 5 is already outside the string.
The start cannot exceed the end
String tekstas = "Java";
System.out.println(tekstas.codePointCount(3, 1));IndexOutOfBoundsException
codePointCount(3, 1) means the range would start after it ends. These bounds make no sense, so Java throws an error.
Handling an error
String tekstas = "Java";
int pradzia = 3;
int pabaiga = 1;
try {
System.out.println(tekstas.codePointCount(pradzia, pabaiga));
} catch (IndexOutOfBoundsException e) {
System.out.println("Netinkamos ribos");
}Netinkamos ribos
Usually, check the bounds before calling: pradzia >= 0 && pradzia <= pabaiga && pabaiga <= tekstas.length(). try-catch is useful when bounds come from user input or another external source.
Common mistakes
endIndexis treated as inclusive, although it marks the position after the last countedcharunit.codePointCount()is confused withlength(): the first counts Unicode code points; the second counts UTF-16 units.- Forgetting that an emoji may occupy two
charunits but be one code point. - Passing bounds in the wrong order:
beginIndexgreater thanendIndex. - Calling the method with a negative index or an
endIndexgreater thanlength().
When should you use codePointCount()?
codePointCount() Use it to count Unicode code points in all or part of a string: validating input length, processing emoji, limiting text length or writing Unicode-aware logic. If the UTF-16 unit count is enough, use length().