Java.lang.String.offsetByCodePoints() Method

Advertisements


Description

The java.lang.String.offsetByCodePoints() method returns the index within this String that is offset from the given index by codePointOffset code points.

Declaration

Following is the declaration for java.lang.String.offsetByCodePoints() method

public int offsetByCodePoints(int index, int codePointOffset)

Parameters

  • index -- This is the index to be offset.

  • codePointOffset -- This is the offset in code points.

Return Value

This method returns the index within this String.

Exception

  • IndexOutOfBoundsException -- if index is negative or larger then the length of this String, or if codePointOffset is positive and the substring starting with index has fewer than codePointOffset code points, or if codePointOffset is negative and the substring before index has fewer than the absolute value of codePointOffset code points.

Example

The following example shows the usage of java.lang.String.offsetByCodePoints() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    String str = "aacdefaa";
    System.out.println("string = " + str);

    // returns the index within this String
    int retval = str.offsetByCodePoints(2, 4);
    // prints the index
    System.out.println("index = " + retval);
  }
}

Let us compile and run the above program, this will produce the following result:

string = aacdefaa
index = 6



Advertisements
Advertisements