Java StringBuilder codePointCount() Method
Example
Return the number of Unicode values in the first 5 characters:
StringBuilder sb = new StringBuilder("Hello");
int result = sb.codePointCount(0, 5);
System.out.println(result);
Definition and Usage
The codePointCount() method returns the number of Unicode values (code points) between two positions.
For most text, this is the same as the number of characters. It can be smaller for special characters, like some emoji, that use two char values.
Syntax
public int codePointCount(int beginIndex, int endIndex)
Parameter Values
| Parameter | Description |
|---|---|
| beginIndex | Required. The index of the first character. |
| endIndex | Required. The index after the last character. |
Technical Details
| Returns: | An int value, representing the number of Unicode values between the two positions. |
|---|---|
| Throws: | IndexOutOfBoundsException - if beginIndex is negative, endIndex is larger than the length, or beginIndex is larger than endIndex. |
Related Pages
Java String codePointCount() Method
❮ StringBuilder Methods