Java String offsetByCodePoints() Method



The Java String offsetByCodePoints() method returns the index within a String object which has been offset from the specified index by codePointOffset code points.

A code point is a numeric value within the Unicode system used to identify a symbol. An offset is the first index of the storage that is being used.

Unpaired surrogates within the text range are to be considered as a one code point each.

Syntax

Following is the syntax for Java String offsetByCodePoints() method −

public int offsetByCodePoints(int index, int codePointOffset)

Parameters

  • regex − 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.

Example

The following example shows the usage of Java String offsetByCodePoints() method. Here we are creating a String with the value 'welcome to tutorialspoint'. Thereafter, we are trying to print the index within the given string.

import java.lang.*; 
public class StringDemo {
   public static void main(String[] args) {
      String str = "welcome to tutorialspoint";
      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);
   }
}

Output

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

string = welcome to tutorialspoint
index = 6

Example

If we pass the index value as zero to this method, it returns the string from the beginning.

In the following example length of the given sequence of string 'Tutorials Point is a reputed firm!' is counted by providing the index of 0 −

public class StringDemo {
   public static void main(String[] args) {
      
      // Initializing a CharSequence object.
      CharSequence c = "Tutorials Point is a reputed firm!";
      System.out.println("The given string is: " + c);
      
      // Initializing the index and codePointOffset
      int Index = 0;
      int codePointOffset = c.length();
      
      // Getting the index within the given character sequence.
      int res = Character.offsetByCodePoints(c, Index, codePointOffset);
      System.out.println("The resultant length of the string is: " + res);
   }
}

Output

Let us compile and run the program above, the output will be displayed as follows −

The given string is: Tutorials Point is a reputed firm!
The resultant length of the string is: 34

Example

If the index is negative or greater than the length of the given sequence, the offsetByCodePoints() method throws an exception, as shown in the example below −

public class StringDemo {
   public static void main(String[] args) {
      
      // initializing the string buffer object
      StringBuffer x = new StringBuffer("Coding");
      System.out.println("The string is: " + x);
      
      // getting the offsetByCodePoints on the index -6 and an offset of 8
      int index = x.offsetByCodePoints(-6, 8);
      System.out.println("index of the offset -6, 8 is: " + index);
   }
}

Exception

On executing the program above, the output is obtained as follows −

The string is: Coding
Exception in thread "main" java.lang.IndexOutOfBoundsException
      at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461)
      at java.base/java.lang.StringBuffer.offsetByCodePoints(StringBuffer.java:279)
      at StringDemo.main(StringDemo.java:7)

Example

Following example shows that if the codePointOffset is positive or negative and the index in the subsequence is smaller than the codePointOffset code point, the offsetByCodePoints() method throws an exception −

public class StringDemo {
   public static void main(String[] args) {
      
      // initializing the string buffer object
      StringBuffer x = new StringBuffer("Coding");
      System.out.println("The string is: " + x);
      
      // getting the offsetByCodePoints on the index -6 and an offset of 8
      int index = x.offsetByCodePoints(2, 5);
      System.out.println("index of the offset 2, 5 is: " + index);
   }
}

Exception

The output for the program above is obtained as follows −

The string is: Coding
Exception in thread "main" java.lang.IndexOutOfBoundsException
      at java.base/java.lang.Character.offsetByCodePoints(Character.java:9342)
      at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:463)
      at java.base/java.lang.StringBuffer.offsetByCodePoints(StringBuffer.java:279)
      at StringDemo.main(StringDemo.java:7)
java_lang_string.htm
Advertisements