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 StringBuilder codePointCount()

By Chaitanya Singh | Filed Under: java

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

Syntax of codePointCount() Method:

int cpCount = sb.codePointCount(2, 5);

The above statement would return the code point count for the characters between index 2 and index 5 in this sequence.

codePointCount() Description

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

codePointCount() Parameters

This method takes two parameters:

  • beginIndex: Integer Index value that represents the start of the range.
  • endIndex: Integer Index value, it represents the 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: Number of Unicode code points in whole sequence

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Welcome\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 Point Count for whole Sequence: "+cpCount);
  }
}

Output:

Java StringBuilder codePointCount() Example Output 1

Example 2: Code Point count for a sub sequence

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

Output:

Java StringBuilder codePointCount() Example Output 2

Example 3: If beginIndex > endIndex

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

Output:

Java StringBuilder codePointCount() Example Output 3

Recommended Posts

  • Java StringBuilder appendCodePoint()
  • Java StringBuilder codePointAt()
  • Java StringBuilder subSequence()
  • Java StringBuilder setCharAt()

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