Java String codePointCount() method with example

Java 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. Thus the length (in chars) of the text range is endIndex-beginIndex. Unpaired surrogates within the text range count as one code point each.

Method Signature

public int codePointCount(int beginIndex,
int endIndex)


beginIndex – the index to the first char of the text range.

endIndex – the index after the last char of the text range.


the number of Unicode code points in the specified text range

Throws Exception:

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



String codePointCount() method Implementation

public int codePointCount(int beginIndex, int endIndex) {
        if (beginIndex < 0 || endIndex > value.length || beginIndex > endIndex) {
            throw new IndexOutOfBoundsException();
        return Character.codePointCountImpl(value, beginIndex, endIndex - beginIndex);

Java String codePointCount() method example


public class CodePointCountExample {

	public static void main(String[] args) {

		String s1="ehowtonow";  
		System.out.println("String : "+s1);
		int codePointCount = s1.codePointCount(1, 9);
		System.out.println("codePointCount between 1 - 9 : "+codePointCount);
		codePointCount = s1.codePointCount(2,6);
		System.out.println("codePointCount between 2 - 6 : "+codePointCount);
		System.out.println("codePointCount between 0 - 9  & 1 - 10 will throw IndexOutOfBoundsException ");
		codePointCount = s1.codePointCount(0, 9);
		codePointCount = s1.codePointCount(1, 10);




String : ehowtonow
codePointCount between 1 - 9 : 8
codePointCount between 2 - 6 : 4
codePointCount between 0 - 9  & 1 - 10 will throw IndexOutOfBoundsException 
Exception in thread "main" java.lang.IndexOutOfBoundsException
	at java.lang.String.codePointCount(

Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java String indexOf(String str, int fromIndex) method with example
  2. How to get Character unicode point in a String
  3. How to convert a String to Lower case
  4. Java String Compare
  5. How to Split the String in Java
  6. Java String indexOf(int ch) method with example
  7. Java String codePointBefore() method with example
  8. How to convert a String to Upper case
  9. How to format the String by using Locale
  10. Java String concat() method with example
  11. How to search a word inside String
  12. Java String codePointCount() method with example
  13. Java String compareToIgnoreCase() method with example
  14. Java String indexOf(int ch, int fromIndex) method with example
  15. Java String codePointAt() method with example

Be the first to comment

Leave a Reply

Your email address will not be published.