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);
Console output

4

  1. The variable tekstas stores the value Java.
  2. 0 is the start of the range.
  3. tekstas.length() is the position after the last char unit.
  4. 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)

Parts of the codePointCount() method
PartMeaning
Calling objecttekstas in the line tekstas.codePointCount(0, tekstas.length())
First parameterbeginIndex – the inclusive start of the range
Second parameterendIndex – the exclusive end of the range
Return typeint
Valid bounds0 <= beginIndex <= endIndex <= length()
ErrorIndexOutOfBoundsExceptionwhen 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()));
Console output

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()));
Console output

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));
Console output

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));
Console output

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));
Result

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));
Result

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));
Result

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");
}
Console output

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

  • endIndex is treated as inclusive, although it marks the position after the last counted char unit.
  • codePointCount() is confused with length(): the first counts Unicode code points; the second counts UTF-16 units.
  • Forgetting that an emoji may occupy two char units but be one code point.
  • Passing bounds in the wrong order: beginIndex greater than endIndex.
  • Calling the method with a negative index or an endIndexgreater than length().

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().