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

By Chaitanya Singh | Filed Under: java

Java StringBuffer codePointBefore(int index) method returns the unicode code point value 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. Here, sb is an object of StringBuffer class.

Syntax of codePointBefore() method

int codePoint = sb.codePointBefore(1);

This statement will return the code point value 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()

Example 1: Code Point value for the first char

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Hello\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 StringBuffer codePointBefore() Output1

Example 2: Code Point for surrogate character

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

Output:

Java StringBuffer codePointBefore() Output2

Example 3: If the desired char is out of range

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

Output:

Java StringBuffer codePointBefore() Output3

Recommended Posts

  • Java StringBuffer codePointCount()
  • Java StringBuffer subSequence()
  • Java StringBuffer setCharAt()
  • Java StringBuffer codePointAt()
❮ 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