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 contains() method with example
  2. Java String equalsIgnoreCase() method with example
  3. Java String codePointAt() method with example
  4. How to format the String by using Locale
  5. Java String codePointCount() method with example
  6. How to get Character unicode point in a String
  7. String Concatenation in Java
  8. Remove the particular character from string
  9. Java String charAt() method with example
  10. Java String copyValueOf(char data[]) method with example
  11. Java String compareTo() method with example
  12. Java String concat() method with example
  13. Java String contentEquals(StringBuffer sb) method with example
  14. Java String contentEquals(CharSequence cs) method with example
  15. Java String indexOf(int ch) method with example

Be the first to comment

Leave a Reply