java.lang.Character offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset)

Description

The Character.offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) java method 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 which means the we would be calling this method in this format:

Character.offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java offsetByCodePoints() method non statically.

Notes:

This method throws the following exception:

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

Method Syntax

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

Method Argument

Data Type Parameter Description
char[] a the char array
int start the index of the first char of the subarray
int count the length of the subarray in chars
int index the index to be offset
int codePointOffset the offset in code points

Method Returns

The offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) method of Character class returns the index within the subarray.

Compatibility

Requires Java 1.5 and up

Java Character offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) Example

Below is a simple java example on the usage of offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) method of Character class.

package com.javatutorialhq.java.examples;

/*
 * This example source code demonstrates the use of 
 * offsetByCodePoints(char[] a,int start,int count,int index,int codePointOffset)
 * method of Character class.
 */

public class CharacterOffsetByCodePointsCharArrayExample {

	public static void main(String[] args) {

		// initialize a test string
		String s = "This is a test string!";
		// convert string to char array
		char[] cs = s.toCharArray();

		// variables
		int startValue = 0;
		int countValue = 5;
		int index = 1;
		int codePointOffset = 4;

		/*
		 * get the index within the given char subarray that is offset from the
		 * given index by codePointOffset code points
		 */

		int result = Character.offsetByCodePoints(cs, startValue, 
				countValue, index, codePointOffset);

		System.out.println("Result:" + result);
	}

}

Sample Output

Below is the sample output when you run the above example.

java Character offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) example output