Java StringBuffer codePointCount() Method



The Java StringBuffer 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 StringBuffer 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

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 StringBuffer codePointCount() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBufferDemo {

   public static void main(String[] args) {

      StringBuffer buff = new StringBuffer("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 StringBuffer("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 StringBuffer("@#$%^&");
      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

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

import java.lang.*;

public class StringBufferDemo {

   public static void main(String[] args) {

      StringBuffer buff = new StringBuffer("/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

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

import java.lang.*;

public class StringBufferDemo {

   public static void main(String[] args) {

      StringBuffer buff = new StringBuffer("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.StringBuffer.codePointCount(StringBuffer.java:227)at StringBufferDemo.main(StringBufferDemo.java:11)
java_lang_stringbuffer.htm
Advertisements