Character.offsetByCodePoints() Method in java
The Character.offsetByCodePoints(CharSequence seq, int index, int codePointOffset) is an inbuilt method in java that 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.
Syntax:
public static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset)
Parameters:
- seq – the char sequence
- index – the index to be offset
- codePointOffset – the offset in code points
Return value: This method of Character class returns the index within the char sequence.
Exceptions:
- NullPointerException – if seq is null.
- IndexOutOfBoundsException – if index is negative or larger than 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.
Below are the programs to illustrate the above mentioned method:
Program 1:
Java
import java.lang.*;
public class gfg {
public static void main(String[] args)
{
CharSequence s = "Hello World";
String str = "The index within the char sequence s is " +
Character.offsetByCodePoints(s, 2 , 6 );
System.out.println(str);
}
}
|
Output:
The index within the char sequence s is 8
Program 2:
Java
import java.lang.*;
public class gfg {
public static void main(String[] args)
{
CharSequence s = "geeks for geeks";
String str = "The index within the char sequence s is " +
Character.offsetByCodePoints(s, 3 , 8 );
System.out.println(str);
}
}
|
Output:
The index within the char sequence s is 11
Last Updated :
19 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...