In Java, Character.codePointCount(seq, beginIndex, endIndex) returns how many code points are in the chars from beginIndex up to (but not including) endIndex. A valid surrogate pair counts as one, and every unpaired surrogate counts as one too.
This is the number a person would call "characters", unlike length(), which counts chars. The char array overload takes an offset and a count instead of two indexes.
A static method of java.lang.Character (Java 5+): call it on the class, as in Character.codePointCount(...); you do not need a Character object. No import is needed, because java.lang is always available.
Syntax
For a CharSequence, pass begin and end indexes; for a char[], pass an offset and a count.
Java
String s = "A\uD83D\uDE00B";
int all = Character.codePointCount(s, 0, s.length()); // 3
int head = Character.codePointCount(s, 0, 2); // 2: 'A' + a lone high surrogate
int pair = Character.codePointCount(s, 1, 3); // 1: the emoji
int arr = Character.codePointCount(s.toCharArray(), 1, 3); // 2: chars 1..3 = emoji + 'B'
Call
What it does
Character.codePointCount(s, 0, s.length())
Characters in the whole text.
Character.codePointCount(seq, from, to)
Characters in a range; to is exclusive.
Character.codePointCount(chars, offset, count)
Characters in count chars starting at offset.
Note the different meaning of the third argument: an end index for CharSequence, a count of chars for char[]. String also has its own codePointCount(begin, end).
Library declarations
Java
// java.lang.Character (since Java 5)
public static int codePointCount(CharSequence seq, int beginIndex, int endIndex)
public static int codePointCount(char[] a, int offset, int count)
Parameters
Parameter
Description
seq / a
The text. Must not be null.
beginIndex, endIndex
Sequence overload: the char range, end exclusive.
offset, count
Array overload: the first char and how many chars to examine.
Return Value
Return Value
Description
int
The number of code points in the range, from 0 up to the number of chars in it.