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 codePointBefore()

By Chaitanya Singh | Filed Under: java

Java StringBuilder codePointBefore(int index) method returns the unicode code point for the character before the specified index. For example, sb.codePointBefore(5) would return the code point for character present at the index 4 in the given sequence sb.

Syntax of codePointBefore() Method:

int codePoint = sb.codePointBefore(1);

The above statement would return the code point for the first character in the sequence.

codePointBefore() Description

public int codePointBefore(int index): Returns the code point for the character present at index - 1 position.

codePointBefore() Parameters

It takes a single parameter:

  • index: Integer index value, it specifies a position in the sequence. The code point for the character just before this position is returned.

codePointBefore() Return Value

  • Returns code point value for the character before the given index.

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

  • index < 1
  • index > sb.length()

Also Read: StringBuilder in Java

Example 1: Code Point value for the first char in the sequence

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Welcome\uD83D\uDE0A");
    //code point for first character
    int codePoint = sb.codePointBefore(1);
    System.out.println("Code Point value for the first char: "+codePoint);
  }
}

Output:

Java StringBuilder codePointBefore() Example Output_1

Example 2: Code Point for surrogate range

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Welcome\uD83D\uDE0A");
    //code point for last char
    int codePoint = sb.codePointBefore(sb.length());
    System.out.println("Code Point value for surrogate: "+codePoint);
  }
}

Output:

Java StringBuilder codePointBefore() Example Output_2

Example 3: If specified index is 0

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Text");
    //if index is 0
    int codePoint = sb.codePointBefore(0);
    System.out.println(codePoint);
  }
}

Output: Exception occurred as it tried to print code point for -1 index which didn’t exist.

Java StringBuilder codePointBefore() Example Output_3

Recommended Posts

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

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