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

By Chaitanya Singh | Filed Under: java

Java StringBuffer offsetByCodePoints(int index, int codePointOffset) method returns the index of a character that is offset from the given index by the specified code points.

Syntax of offsetByCodePoints() method

int index = sb.offsetByCodePoints(3, 4);

The above statement will return the index of a character that is 4 code points away from the character present at index 3.

offsetByCodePoints() Description

public int offsetByCodePoints(int index, int codePointOffset): Returns the index of a character that is specified code points away from the character present at the given index.

offsetByCodePoints() Parameters

It takes two parameters:

  • index: Gives the starting position. Integer index value that specifies a char in sequence. The offset count starts from here.
  • codePointOffset: It is the integer offset value in code points.

offsetByCodePoints() Return Value

  • Returns an index of character in the sequence represented by objected of StringBuffer class.

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

  • If index < 0 or > sb.length()
  • If codePointOffset is positive and index + codePointOffset > sb.length()
  • If codePointOffset is negative and index + codePointOffset < 0

Example 1: Index of an offset character

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Welcome");
    //starting index 0, offset 2
    int index = sb.offsetByCodePoints(0,2);
    //Index of char that is 2 code points away from first char
    System.out.println("Index of offset char: "+index);
  }
}

Output:

Java StringBuffer offsetByCodePoints() Output 1

Example 2: If codePointOffset is negative

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Welcome");
    //starting index 5, offset -2
    int index = sb.offsetByCodePoints(5,-2);
    System.out.println("Index of offset char: "+index);
  }
}

Output:

Java StringBuffer offsetByCodePoints() Output 2

Example 3: If sum of offset and code points is greater than length

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Welcome");
    //starting index 5, offset 5
    int index = sb.offsetByCodePoints(5,5);
    System.out.println("Index of offset char: "+index);
  }
}

Output:

Java StringBuffer offsetByCodePoints() Output 3

Recommended Posts

  • Java StringBuffer charAt()
  • Java StringBuffer ensureCapacity()
  • Java StringBuffer capacity()
  • Java StringBuffer toString()
❮ 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