Java StringBuilder codePointCount() Method



The Java StringBuilder codePointCount() method counts the number of Unicode code points in the specified text range of this sequence. The text range begins at the specified starting index and extends to the char at second to the last index. Thus the length (in chars) of the text range is ending index – beginning index.

If there is no Unicode code point present in the given range of text, the method will not throw any errors and just prints ‘0’.

Note − Unpaired surrogate code points are considered one separate code point each.

Syntax

Following is the syntax for Java StringBuilder codePointCount() method

public int codePointCount(int beginIndex, int endIndex)

Parameters

  • 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.

Example: Getting length of Text Range

When we consider the input text as the alphabet, the method returns the length of the text range given as arguments.

The following example shows the usage of Java StringBuilder codePointCount() method.

package com.tutorialspoint;

public class StringBuilderDemo {

   public static void main(String[] args) {

      StringBuilder buff = new StringBuilder("TUTORIALS");
      System.out.println("buffer = " + buff);

      // returns the codepoint count from index 1 to 5
      int retval = buff.codePointCount(1, 5);
      System.out.println("Count = " + retval);
    
      buff = new StringBuilder("7489042 ");
      System.out.println("buffer = " + buff);
      
      // returns the codepoint count from index 3 to 9
      retval = buff.codePointCount(3, 9);
      System.out.println("Count = " + retval);

      buff = new StringBuilder("@#$%^&");
      System.out.println("buffer = " + buff);
      
      // returns the codepoint count from index 2 to 4
      retval = buff.codePointCount(2, 4);
      System.out.println("Count = " + retval);
   }
}

Output

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

buffer = TUTORIALS
Count = 4
buffer = 7489042
Count = 6
buffer = @#$%^&
Count = 2

Example: Getting length of Text Range having no Valid CodePoints

When we consider the input text as characters that do not have valid codepoints, the method returns zero.

public class StringBuilderDemo {

   public static void main(String[] args) {

      StringBuilder buff = new StringBuilder("/u1298139");
      System.out.println("buffer = " + buff);

      // returns the codepoint count
      int retval = buff.codePointCount(0, 0);
      System.out.println("Count = " + retval);
   }
}

Output

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

buffer = /u1298139
Count = 0

Example: Facing Exception while checking count of CodePoints

However, if the index arguments given exceed or precede the text range, the method throws an IndexOutOfBounds Exception.

public class StringBuilderDemo {

   public static void main(String[] args) {

      StringBuilder buff = new StringBuilder("djk137");
      System.out.println("buffer = " + buff);
      
      // returns the codepoint count from index 2 to 4
      int retval = buff.codePointCount(-1, 9);
      System.out.println("Count = " + retval);

   }
}

Exception

If we compile and run the program, an IndexOutOfBounds Exception is thrown instead of printing the output −

buffer = djk137
Exception in thread "main" java.lang.IndexOutOfBoundsException
at java.lang.AbstractStringBuilder.codePointCount(AbstractStringBuilder.java:320)
	at java.lang.StringBuilder.codePointCount(StringBuilder.java:227)at StringBuilderDemo.main(StringBuilderDemo.java:11)
java_lang_stringbuilder.htm
Advertisements