GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — StringBuffer codePointCount() method in Java with Examples - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

StringBuffer codePointCount() method in Java with Examples

Last Updated : 30 Jul, 2019
The codePointCount() method of StringBuffer class is used to return the number of Unicode code points in the specified range of beginIndex to endIndex of String contained by StringBuffer. This method takes beginIndex and endIndex as a parameter where beginIndex is the index of the first character of the text range and endIndex is index after the last character of the text range. The indexes refer to char values (Unicode code units) and the value of index must be lie between 0 to length-1. The range starts at the beginIndex and end at the char at index endIndex – 1. Thus the length (in chars) of the text range is endIndex-beginIndex. Syntax:
public int codePointCount(int beginIndex,
                               int endIndex)
Parameters: This method takes two parameters:
  • beginIndex: int value representing index of the first character of the text range.
  • endIndex: int value representing index after the last character of the text range.
Return Value: This method returns int value representing the number of Unicode code points in the specified text range. Exception: This method throws IndexOutOfBoundsException if:
  • the beginIndex is less than zero,
  • or endIndex is larger than the length of String,
  • or beginIndex is larger than endIndex.
Below programs illustrate the StringBuffer.codePointCount() method: Example 1: Java
// Java program to demonstrate
// the codePointCount() Method.

class GFG {
    public static void main(String[] args)
    {

        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer
            str
            = new StringBuffer("Welcome to GeeksforGeeks");

        // print string
        System.out.println("String = " + str.toString());

        // returns the codepoint count from index 4 to 10
        int codepoints = str.codePointCount(4, 10);

        System.out.println("No of Unicode code points "
                           + " between index 4 and index 10 = "
                           + +codepoints);
    }
}
Output:
String = Welcome to GeeksforGeeks
No of Unicode code points  between index 4 and index 10 = 6
Example 2: Java
// Java program to demonstrate
// the codePointCount() Method.

class GFG {
    public static void main(String[] args)
    {

        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer
            str
            = new StringBuffer("GeeksForGeeks contribute");

        // print string
        System.out.println("String = "
                           + str.toString());

        // returns the codepoint count
        // from index 3 to 7
        int
            codepoints
            = str.codePointCount(13, 17);

        System.out.println("No of Unicode code points"
                           + " between index 13 and 17 = "
                           + codepoints);
    }
}
Output:
String = GeeksForGeeks contribute
No of Unicode code points between index 13 and 17 = 4
Example 3: To demonstrate IndexOutOfBoundsException Java
// Java program to demonstrate
// exception thrown by the codePointCount() Method.

class GFG {
    public static void main(String[] args)
    {

        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer
            str
            = new StringBuffer("GeeksForGeeks");

        try {

            // make beginIndex greater than endIndex
            int codepoints = str.codePointCount(2, 0);
        }

        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
Output:
Exception: java.lang.IndexOutOfBoundsException
References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#codePointCount(int, int)
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.