Facebook Page Twitter Page LinkedIn Page
× Java Tutorial Java Advanced Java Resources


The Java string offsetByCodePoints() method returns the index within the given string that is offset from the given index by codePointOffset code points.

Syntax

public int offsetByCodePoints(int index,
                              int codePointOffset)

Parameters

index Specify the index to be offset.
codePointOffset Specify codePointOffset, the offset in code points.

Return Value

Returns the index within the given string.

Exception

throws IndexOutOfBoundsException, if index is negative or larger then the length of the 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:

In the below example, offsetByCodePoints() method returns the index within the given string which is offset from the given index by specified codePointOffset code points.

public class MyClass {
  public static void main(String[] args) {
    String MyString = "Hello World!.";
    
    //returns the index within the string
    int val = MyString.offsetByCodePoints(3, 5);
    System.out.println("index = " + val); 
  }
}

The output of the above code will be:

index = 8

❮ Java String Methods