Java.lang.StringBuilder.codePointCount() Method
Description
The java.lang.StringBuilder.codePointCount() method returns the number of Unicode code points in the specified text range of this sequence.
The text range begins at the specified beginIndex and extends to the char at index endIndex - 1. Therefore the length (in chars) of the text range is endIndex - beginIndex
Declaration
Following is the declaration for java.lang.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.
Exception
IndexOutOfBoundsException -- if the beginIndex is negative, or endIndex is larger than the length of this sequence, or beginIndex is larger than endIndex.
Example
The following example shows the usage of java.lang.StringBuilder.codePointCount() method.
package com.tutorialspoint;
import java.lang.*;
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder str = new StringBuilder("programming");
System.out.println("string = " + str);
// returns the codepoint count from index 1 to 5
int retval = str.codePointCount(1, 5);
System.out.println("Count = " + retval);
str = new StringBuilder("amrood admin ");
System.out.println("string = " + str);
// returns the codepoint count from index 3 to 9
retval = str.codePointCount(3, 9);
System.out.println("Count = " + retval);
}
}
Let us compile and run the above program, this will produce the following result:
string = programming Count = 4 string = amrood admin Count = 6