codePointCount() - String Methods in Java


This article is a part of String Methods in Java.
This method returns the Unicode values found in the specified range of the given string. It contains two parameters namely, startIndex and endIndex which specifies the beginning and ending of the search respectively.


Return type

The return type of codePointCount() method is int. Let us look at an example code to understand this method.

Example


Output

Let us look at the output of the above example code.

String Methods codePointCount()
Fig.1- String Method - codePointCount()

Here the characters between the index 1 and 6 are, “e,” “a,” “r,” “n,” “i.” Each of these characters has one Unicode value attached to it.

IndexOutOfBound exception in codePointCount()

When we pass negative index or outside the range specified then the codePointCount() method throws IndexOutOfBound exception. Let us look at the code for this case.


Output

Let us look at the output of the above example code.

String Methods codePointCount() IndexOutOfBound
Fig.2- String Method - codePointCount() IndexOutOfBound

Since the value 20 specified as the endIndex is way beyond the range of the string, the method throws an exception.

Read More

Author : Satyam Kumar -



You will also like: