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 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.
Below are the programs to illustrate the above mentioned method:
Program 1:
// Code to illustrate the offsetByCodePoints() method import java.lang.*; public class gfg { public static void main(String[] args) { // Create a CharSequence s and assign value CharSequence s = "Hello World"; // Result of offsetByCodePoints on s String str = "The index within the char sequence s is " + Character.offsetByCodePoints(s, 2, 6); // Print str value System.out.println(str); } } |
The index within the char sequence s is 8
Program 2:
// Code to illustrate the offsetByCodePoints() method import java.lang.*; public class gfg { public static void main(String[] args) { // Create a CharSequence s and assign value CharSequence s = "geeks for geeks"; // Result of offsetByCodePoints on s String str = "The index within the char sequence s is " + Character.offsetByCodePoints(s, 3, 8); // Print str value System.out.println(str); } } |
The index within the char sequence s is 11
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.string.replace() method in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.math.BigInteger.modInverse() method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Method Class | getName() Method in Java
- Method Class | toGenericString() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

