Java String codePointCount() Method Tutorial

In this section, we will learn what the String codePointCount() method is and how to use it in Java.

What is Java String codePointCount() Method?

The codePointCount() method is used to return the number of Unicode values that is between the specified range in a string value.

Java codePointCount() Method Syntax:

public int codePointCount(int startIndex, int endIndex)

codePointCount() Method Parameters:

The first argument of the method declares the beginning index and the second argument declares the end index of the range that we want to search.

You should know that the second argument is exclusive. This means if the value is 5 for example, the search is to the index number 4 and not 5.

codePointCount() Method Return Value:

the return value of this method is an integer number which is the number of Unicodes between the specified range.

codePointCount() Method Exceptions:

  • The value we set as the argument of the method should be in the range of the length of the target String. Otherwise we will get the  StringIndexOutOfBoundsException exception.
  • The first element of a string is at index 0 and the last one is at index (length-1). So make sure the number is in the range.

Example: using String codePointCount() method

public class Simple {

    public static void main(String[] args) {
        String s = "Hello";
        int unicode = s.codePointCount(1,5);
Output: 4


Top Technologies