public int codePointCount(int beginIndex, int endIndex);
Parameters
Parameter
Description
beginIndex
Required. Where the range starts.
endIndex
Required. Where the range ends. This index is exclusive.
Return Value
Return Value
Description
int
How many Unicode code points are in the range.
Java Examples
Example 1 — Whole-String Count vs length()
For ASCII-only text the two numbers match. With emoji, codePointCount is the fairer “character” count.
java
public class CodePointCountBasic {
public static void main(String[] args) {
String text = "Hi😀";
int units = text.length();
int points = text.codePointCount(0, text.length());
System.out.println("length() = " + units);
System.out.println("codePointCount = " + points);
}
}
Output
length() = 4
codePointCount = 3
How It Works
Store the text."Hi😀" has two letters plus one emoji.
Measure storage.length() returns 4 because the emoji uses two UTF-16 units.
Count code points.codePointCount(0, length()) walks the full range.
Get 3. Each of H, i, and 😀 contributes one code point.
Example 2 — Count Only Part of the String
Pass a smaller range to count just a slice—useful with codePointAt when you already know UTF-16 bounds.
java
public class CodePointCountRange {
public static void main(String[] args) {
String text = "Hi😀!";
int letters = text.codePointCount(0, 2);
int emoji = text.codePointCount(2, 4);
int bang = text.codePointCount(4, 5);
System.out.println("letters = " + letters);
System.out.println("emoji = " + emoji);
System.out.println("bang = " + bang);
}
}
Output
letters = 2
emoji = 1
bang = 1
How It Works
Map indexes.H/i at 0–1; emoji at 2–3; '!' at 4. The end index is exclusive.
Count the prefix.codePointCount(0, 2) covers indexes 0 and 1 → two BMP letters → 2.
Count the emoji slice.codePointCount(2, 4) covers indexes 2 and 3 → one surrogate pair → 1.
Count the suffix.codePointCount(4, 5) covers index 4 → a single '!' → 1.
Example 3 — Enforce a Character Limit
Validate user input by code points, not UTF-16 length— so one emoji does not use up two “slots.” To move an index by N characters, see offsetByCodePoints().
java
public class CodePointCountLimit {
public static boolean withinLimit(String text, int maxChars) {
if (text == null) {
return false;
}
return text.codePointCount(0, text.length()) <= maxChars;
}
public static void main(String[] args) {
System.out.println(withinLimit("Hi😀", 3)); // true
System.out.println(withinLimit("Hi😀", 2)); // false
System.out.println(withinLimit("Java", 4)); // true
}
}
Output
true
false
true
How It Works
Guard null. A null string fails the check immediately.
Count code points. Full-range codePointCount gives the character total.
Compare to the limit."Hi😀" is 3 points—allowed when max is 3, rejected when max is 2.
ASCII still works."Java" has 4 points and 4 units, so both views agree.
Frequently Asked Questions
codePointCount() counts Unicode code points between beginIndex and endIndex.
Both are required. beginIndex starts the range. endIndex ends it and is exclusive.