Java.lang.String.codePointCount() Method



The java.lang.String.codePointCount() method returns the number of Unicode code points in the specified text range of this String. The text range begins at the specified beginIndex and extends to the char at index endIndex - 1. Therefore the length (in chars) of the text range is endIndex-beginIndex.


Following is the declaration for java.lang.String.codePointCount() method

public int codePointCount(int beginIndex, int endIndex)


  • beginIndex − This is the index to the first char of the text range.

  • endIndex − This is the index after the last char of the text range.

Return Value

This method returns the number of Unicode code points in the specified text range.


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


The following example shows the usage of java.lang.String.codePointCount() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

   public static void main(String[] args) {

      String str = "JAVA programming language";
      System.out.println("String = " + str);

      // codepoint from index 1 to index 8
      int retval = str.codePointCount(1, 8);

      // prints character from index 1 to index 8
      System.out.println("Codepoint count = " + retval);

Let us compile and run the above program, this will produce the following result −

String = JAVA programming language
Codepoint count = 7