Home » Java programming language

Java StringBuilder offsetByCodePoints() method with example

StringBuilder Class offsetByCodePoints() method: Here, we are going to learn about the offsetByCodePoints() method of StringBuilder Class with its syntax and example.
Submitted by Preeti Jain, on December 22, 2019

StringBuilder Class offsetByCodePoints() method

  • offsetByCodePoints() method is available in java.lang package.
  • offsetByCodePoints() method is used to retrieve the index within the set of character sequence contained by this object that is offset from the indices passed as an argument by code point offset code points.
  • offsetByCodePoints() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • offsetByCodePoints() method may throw an exception at the time of returning an index by code points. IndexOutOfBoundsException – This exception may throw
    • Case 1: when the first parameter indices < 0, greater than length() or beg > end.
    • Case 2: when the second parameter cp_off > 0, subsequence before indices has fewer than cp_off code points.
    • Case 3: when the second parameter cp_off < 0, subsequence before indices has fewer than cp_off code points absolute value.

Syntax:

    public int offsetByCodePoints(int indices, int cp_off);

Parameter(s):

  • int indices – represents the index to be offset
  • int cp_off – represents the code point offset

Return value:

The return type of this method is int, it returns the indices within this sequence.

Example:

// Java program to demonstrate the example 
// of int offsetByCodePoints(int indices, int cp_off)
// method of StringBuilder 

public class OffsetByCodePoints {
    public static void main(String[] args) {
        int indices = 3;
        int cp_off = 7;

        // Creating an StringBuilder object
        StringBuilder st_b = new StringBuilder("Java World ");

        // Display st_b 
        System.out.println("st_b = " + st_b);

        // By using offsetByCodePoints(indices,cp_off) method is to
        // return the index within this range in st_b
        int res = st_b.offsetByCodePoints(indices, cp_off);

        // Display st_b
        System.out.println("st_b.offsetByCodePoints(indices,cp_off) = " + res);
    }
}

Output

st_b = Java World 
st_b.offsetByCodePoints(indices,cp_off) = 10


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.