Home »
Java programming language
Java StringBuilder codePointCount() method with example
StringBuilder Class codePointCount() method: Here, we are going to learn about the codePointCount() method of StringBuilder Class with its syntax and example.
Submitted by Preeti Jain, on December 22, 2019
StringBuilder Class codePointCount() method
- codePointCount() method is available in java.lang package.
- codePointCount() method is used to count the number of Unicode code points lies in the given range beg_idx to end_idx.
- codePointCount() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- codePointCount() method may throw an exception at the time of assigning index.
IndexOutOfBoundsException - This exception may throw when the beg_idx < 0 or end_idx is not less than the length of this array sequence or the other condition is beg_idx > end_idx.
Syntax:
public int codePointCount(int beg_idx , int end_idx);
Parameter(s):
- int beg_idx – represents the starting index of Unicode code point to start.
- int end_idx – represents the ending index of Unicode code point to ends.
Return value:
The return type of this method is int, it returns the counts of the number of Unicode code point of the given range.
Example:
// Java program to demonstrate the example
// of int codePointCount(int beg_idx , int end_idx)
// method of StringBuilder
public class CodePointCount {
public static void main(String[] args) {
// Creating an StringBuilder object
StringBuilder st_b = new StringBuilder("Java World");
System.out.println("st_b = " + st_b);
// By using codePointCount(4,8) method is to count the
// number of codepoint lies in a range from index 4
// to index 8
int cp = st_b.codePointCount(4, 8);
// Display all the codepoints from index 4 to index 8
System.out.println("st_b.codePointCount(4,8) = " + cp);
}
}
Output
st_b = Java World
st_b.codePointCount(4,8) = 4