BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java StringBuffer codePointCount()

By Chaitanya Singh | Filed Under: java

Java StringBuffer codePointCount(int beginIndex, int endIndex) returns the code points count between the given indexes. In this tutorial, we will discuss codePointCount() method with examples.

Syntax of codePointCount() method

int cpCount = sb.codePointCount(3, 6);

This statement will return the code points count for the characters between index 3 and index 6 in this sequence. Here, sb is an object of StringBuffer class.

codePointCount() Description

public int codePointCount(int beginIndex, int endIndex): It looks into the range specified by beginIndex and endIndex. Counts the code points available in the range and returns this count.

codePointCount() Parameters

This method takes two parameters:

  • beginIndex: Int index, it represents start of the range.
  • endIndex: Int Index, it represents end of the range. This index is exclusive so the character present at this index is not included in the code point count.

codePointCount() Return Value

  • Returns the code point counts in the specified range.

It throws IndexOutOfBoundsException, if any of the following condition occurs:

  • beginIndex < 0
  • endIndex > sb.length()
  • beginIndex > endIndex

Example 1: Count code points in a given Sequence

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Hello\uD83D\uDE0A");

    //start index first char
    //end index last char
    //whole sequence is included in the range
    int cpCount = sb.codePointCount(0, sb.length());
    System.out.println("Sequence is: "+sb);
    System.out.println("Code Points Count for whole Sequence: "+cpCount);
  }
}

Output:

Java StringBuffer codePointCount() Output 1

Example 2: Code points count for a subsequence

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Hello\uD83D\uDE0A");
    //from index 5 till end
    int cpCount = sb.codePointCount(5, sb.length());
    System.out.println("Sequence is: "+sb);
    System.out.println("Sub sequence: "+sb.subSequence(5, sb.length()));
    System.out.println("Code Point Count from index 5 till end: "+cpCount);
  }
}

Output:

Java StringBuffer codePointCount() Output 2

Example 3: If beginIndex is greater than endIndex

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Hello\uD83D\uDE0A");
    //if beginIndex > endIndex
    int cpCount = sb.codePointCount(3, 2);
    System.out.println(cpCount);
  }
}

Output:

Java StringBuffer codePointCount() Output 3

Recommended Posts

  • Java StringBuffer subSequence()
  • Java StringBuffer setCharAt()
  • Java StringBuffer codePointAt()
  • Java StringBuffer offsetByCodePoints()
❮ Java StringBuffer

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap