Java Tutorial Java Advanced Java References

Java String - codePointCount() Method



The Java codePointCount() method returns the number of Unicode code points in the specified text range of the string. The text range starts from the specified beginIndex and ends at index endIndex - 1.

Syntax

public int codePointCount(int beginIndex, int endIndex)

Parameters

beginIndex specify the index to the first char of the text range.
endIndex specify the index after the last char of the text range.

Return Value

Returns the number of Unicode code points in the specified text range of the string.

Exception

Throws IndexOutOfBoundsException, if the beginIndex is negative, or endIndex is larger than the length of this String, or beginIndex is larger than endIndex.

Example:

In the example below, codePointCount() method is used to count the Unicode code points in the specified text range of the string called MyString.

public class MyClass {
  public static void main(String[] args) {
    String MyString = "Hello World!.";
    System.out.println(MyString.codePointCount(0, 10));   
  }
}

The output of the above code will be:

10

❮ Java String Methods