java.lang.Character offsetByCodePoints(CharSequence seq, int index, int codePointOffset)

Description

The Character.offsetByCodePoints(CharSequence seq, int index, int codePointOffset) java method returns the index within the given char sequence that is offset from the given index by codePointOffset code points. Unpaired surrogates within the text range given by index and codePointOffset count as one code point each.

The offsetByCodePoints(CharSequence seq, 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(CharSequence seq, 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 seq is null.
  • IndexOutOfBoundsException – if index is negative or larger then the length of the char 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.

Method Syntax

public static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset)

Method Argument

Data Type Parameter Description
CharSequence seq the char sequence
int index the index to be offset
int codePointOffset the offset in code points

Method Returns

The offsetByCodePoints(CharSequence seq, int index, int codePointO method of Character class returns the index within the char sequence.

Compatibility

Requires Java 1.5 and up

Java Character offsetByCodePoints(CharSequence seq, int index, int codePointOffset) Example

Below is a simple java example on the usage of offsetByCodePoints(CharSequence seq, int index, int codePointOffset) method of Character class.

package com.javatutorialhq.java.examples;

/*
 * This example source code demonstrates the use of 
 * offsetByCodePoints(CharSequence seq,int index,int codePointOffset)
 * method of Character class.
 */

public class CharacterOffsetByCodePointsCharSequenceExample {

	public static void main(String[] args) {

		// initialize a new CharSequence object
		CharSequence cs = "This is a test string!";

		// initialize beginIndex and codePointOffset
		int beginIndex = 0;
		int codePointOffset = cs.length();

		/*
		 * get the index within the given char sequence that is 
		 * offset from the given index by codePointOffset code points
		 */
		
		int result = Character.offsetByCodePoints(cs,
				beginIndex, codePointOffset);

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

}

Sample Output

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

java Character offsetByCodePoints(CharSequence seq, int index, int codePointOffset) example output