StringBuilder.offsetByCodePoints() Method



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


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

public int offsetByCodePoints(int index, int codePointOffset)


  • 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 sequence.


IndexOutOfBoundsException − if index is negative or larger then the length of this sequence, or if codePointOffset is positive and the subsequence starting with index has fewer than codePointOffset code points, or if codePointOffset is negative and the subsequence before index has fewer than the absolute value of codePointOffset code points.


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

package com.tutorialspoint;

import java.lang.*;

public class StringBuilderDemo {

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

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

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

string = abcdefg
index = 5