Java StringBuffer offsetByCodePoints() Method



The Java StringBuffer offsetByCodePoints() method is, used to retrieve the index value within this sequence that is offset from the given index by codePointOffset code points.

This method returns a stream of code point values of the sequence character. A stream contains an integer code point values, which is similar to the ASCII value in Java.

The offsetByCodePoints() method accepts two parameters as an integer that holds the values of index and codePointOffset. It throws different exceptions if the index value is negative or greater than the sequence length, and so on.

Syntax

Following is the syntax of the Java StringBuffer 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 sequence.

Example

If the given index value is positive and less than the sequence length, the offsetByCodePoint() method returns the index value.

In the following program, we are creating an object of the StringBuffer class with the value of “TutorialsPoint”. Using the offsetByCodePoints(), we are trying to get the index value at the specified startIndex 2, and the codePointOffset 5.

public class OffsetCodePoint {
   public static void main(String[] args) {
      //create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("TutorialsPoint");
      System.out.println("The given String is: " + sb);
      //initialize the index and codePointOffset values
      int index = 2;
      int codePointOffset = 5;
      System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset);
      //using the offsetByCodePoint() method
      System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset));
   }
}

Output

On executing the above program, it will produce the following result −

The given String is: TutorialsPoint
The given index and code point offset values are: 2 and 5
The index is: 7

Example

If the given index value is negative and greater than the sequence length, the offsetByCodePoint() method throws an IndexOutOfBoundIndex.

If the following program, we are instantiating the StringBuffer class with the value “Java Programming”. Using the offsetByCodePoints() method, we are trying to get the index value at the specified index -1 and codePointOffset 5.

public class OffsetCodePoint {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuffer class
         StringBuffer sb = new StringBuffer("Java Programming");
         System.out.println("The given String is: " + sb);
         //initialize the index and codePointOffset values
         int index = -1;
         int codePointOffset = 5;
         System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset);
         //using the offsetByCodePoint() method
         System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception e: " + e);
      }
   }
}

Output

Following is the output of the above program −

The given String is: Java Programming
The given index and code point offset values are: -1 and 5
java.lang.IndexOutOfBoundsException
   at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461)
   at java.base/java.lang.StringBuffer.offsetByCodePoints(StringBuffer.java:279)
   at OffsetCodePoint.main(OffsetCodePoint.java:15)
Exception e: java.lang.IndexOutOfBoundsException

Example

If the given index value is larger than the sequence length, this method throws the IndexOutOfBoundException.

In the following example, we are creating a StringBuffer with the value “Hello”. Using the offsetByCodePoints() method, we are, trying to get the index value at the specified startIndex 10, and codePointOffset 2.

public class OffsetCodePoint {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuffer class
         StringBuffer sb = new StringBuffer("Hello");
         System.out.println("The given String is: " + sb);
         //initialize the index and codePointOffset values
         int index = 10;
         int codePointOffset = 2;
         System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset);
         //using the offsetByCodePoint() method
         System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception e: " + e);
      }
   }
}

Output

The above program produces the following output −

The given String is: Hello
The given index and code point offset values are: 10 and 2
java.lang.IndexOutOfBoundsException
   at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461)
   at java.base/java.lang.StringBuffer.offsetByCodePoints(StringBuffer.java:279)
   at OffsetCodePoint.main(OffsetCodePoint.java:15)
Exception e: java.lang.IndexOutOfBoundsException
java_lang_stringbuffer.htm
Advertisements