HomeLanguagesJavaCharacter.offsetByCodePoints() in Java with Examples

Character.offsetByCodePoints() in Java with Examples

The Character.offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) is an inbuilt method in Java that returns the index within the given char subarray that is offset from the given index by codePointOffset code points. The start and count arguments specify a subarray of the char array. Unpaired surrogates within the text range given by index and codePointOffset count as one code point each. The offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) method of Character class is static thus it should be accessed statically. A non-static method is usually called by declaring method_name(argument). But in this case, since it is a static method, the class name is appended as a suffix, during the call. A compilation problem may be encountered if the java offsetByCodePoints() method is tried to be called in a non-static manner. 

Syntax:

public static int offsetByCodePoints(char[] a, int start, int count, int index,
int codePointOffset)

Parameters:

  • a: the char array
  • start: the index of the first char of the subarray
  • count: the length of the subarray in chars
  • index: the index to be offset
  • codePointOffset: the offset in code points

Return value: This method returns an integer type value i.e., the index, within the subarray. 

Exceptions:

  • NullPointerException: If a is null.
  • IndexOutOfBoundsException: If start or count is negative, or if start + count is larger than the length of the given array, or if index is less than start or larger than start + count, or if codePointOffset is positive and the text range starting with index and ending with start + count – 1 has fewer than codePointOffset code points, or if codePointOffset is negative and the text range starting with start and ending with index – 1 has fewer than the absolute value of codePointOffset code points.

Below program illustrate the Character.offsetByCodePoints() method: 

Java




//Java program for offsetByCodePoints() method
import java.lang.*;
public class gfg {
   public static void main(String[] args) {
      // Creating a char array c_arr and assigning values
      char[] c_arr = new char[] {'g','e','e','k','s'};
 
      // Integer primitives
      int s = 1;
      int c= 4;
 
      // Creating and assigning result of offsetByCodePoints
      // On subarray of c_arr to r
     int r = Character.offsetByCodePoints(c_arr, s, c, 1, 2);
 
      String st = "The index within the subarray is " + r;
 
      System.out.println(st);
   }
}


Output:

The index within the subarray is 3

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#offsetByCodePoints(char[],%20int,%20int,%20int,%20int)

RELATED ARTICLES

6 COMMENTS

Most Popular

Dominic
32675 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7044 POSTS0 COMMENTS
Nicole Veronica
12183 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12277 POSTS0 COMMENTS
Shaida Kate Naidoo
7167 POSTS0 COMMENTS
Ted Musemwa
7437 POSTS0 COMMENTS
Thapelo Manthata
7143 POSTS0 COMMENTS
Umr Jansen
7135 POSTS0 COMMENTS