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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.