Java offsetByCodePoints Method

The Java offsetByCodePoints method is one of the String Methods, which is to return the index within the string that is offset from the specified index by CodePointOffset code points. The basic syntax of the Java String offsetByCodePoints is as shown below.

public int offsetByCodePoints(int index, int codePointOffset) // It will return the integer Value as Output

//To use in program
String_Object.offsetByCodePoints(int index, int codePointOffset)
  • index: Please specify the index to be offset.
  • codePointOffset: Please specify the offset in code points.

Java offsetByCodePoints Method example

The string.offsetByCodepoints method returns the index within the string offset from the specified index by CodePointOffset code points. In this Java program, we are going to find the same.

package StringFunctions;

public class offsetByCodePointsMethod {
	public static void main(String[] args) {
		String str = "Hello";
		int a = str.offsetByCodePoints(1, 2);
		int b = "Tutorial GateWay".offsetByCodePoints(1, 12);
		int c = "Free Java Tutorial at Tutorial GateWay".offsetByCodePoints(10, 22);
		int d = "Java Programming Language".offsetByCodePoints(5, 12);
		
		System.out.println("The New Index of a String Str = " + a);
		System.out.println("The New Index  = " + b);
		System.out.println("The New Index  = " + c);
		System.out.println("The New Index  = " + d);
	}
}
Java offsetByCodePoints Method 1

In this Java program, First, we declared the String variable and assigned a value using the following statement.

String str = "Hello";

The following statements call the public int offsetByCodePoints(int index, int codePointOffset) method to find the index within the above-specified string.

int a = str.offsetByCodePoints(1, 2);

Next, we apply the offsetByCodepoints String Method directly on the string and then assign new index values of those strings to the integer variables b, c, and d.

int b = "Tutorial GateWay".offsetByCodePoints(1, 12);
int c = "Free Java Tutorial at Tutorial GateWay".offsetByCodePoints(10, 22);
int d = "Java Programming Language".offsetByCodePoints(5, 12);

The following Java System.out.println statements print the output.