- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Character offsetByCodePoints() Method
The Java Character offsetByCodePoints() method is used to retrieve the index within the given char subarray that is offset from the given index by a code point offset.
The subarray is specified in the char array by counting the characters from a certain starting index.
Unpaired surrogates within the text range are to be considered as a one code point each.
This method occurs in two polymorphic forms with same return type but different parameters.
Syntax
Following is the syntax for Java Character offsetByCodePoints() method
public static int offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) (or) public static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset)
Parameters
a − the char array
start − the index of the first char of the subarray
count − the length of the subarray in chars
index − the index to be offset
seq − the char sequence
codePointOffset − the offset in code points
Return Value
This method returns the index within the subarray
Example
The following example shows the usage of Java Character offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create a char array c and assign values char[] c = new char[] { 'a', 'b', 'c', 'd', 'e', 'f' }; // craete 2 int primitives start, count and assign values int start = 1; int count = 5; // create an int primitive res int res; // assign result of offsetByCodePoints on subarray of c to res res = Character.offsetByCodePoints(c, start, count, 2, 4); String str = "The index within the subarray of c is " + res; // print res value System.out.println( str ); } }
Output
Let us compile and run the above program, this will produce the following result −
The index within the subarray of c is 6
Example
The following example shows the usage of Java Character offsetByCodePoints(CharSequence seq, int index, int codePointOffset) method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create a CharSequence seq and assign value CharSequence seq = "Hello World"; // create an int primitive res int res; // assign result of offsetByCodePoints on seq to res res = Character.offsetByCodePoints(seq, 3, 8); String str = "The index within the char sequence seq is " + res; // print res value System.out.println( str ); } }
Output
Let us compile and run the above program, this will produce the following result −
The index within the char sequence seq is 11
Example
Another example to demonstrate the usage of this method by passing a digit char array as the argument is as follows:
import java.lang.*; public class CharacterDemo { public static void main(String[] args) { char[] c = new char[] { '0', '1', '2', '3', '4', '5' }; int start = 1; int count = 3; int result = Character.offsetByCodePoints(c, start, count, 1, 2); System.out.println("The index within the subarray of c is " + result); } }
Output
The output for the program above after compiling and execution is printed as follows −
The index within the subarray of c is 3
Example
There are also cases when this method throws exceptions. In this example, we will see a scenario where the method throws an IndexOutOfBounds Exception.
import java.lang.*; public class CharacterDemo { public static void main(String[] args) { char[] c = new char[] { '0', '5', '2', 'a', 'z', '/' }; int start = 0; int count = 3; int result = Character.offsetByCodePoints(c, start, count, 2, 4); System.out.println("The index within the subarray of c is " + result); } }
Exception
After compiling and running the program above, the output is obtained as follows −
Exception in thread "main" java.lang.IndexOutOfBoundsExceptionat java.lang.Character.offsetByCodePointsImpl(Character.java:5388) at java.lang.Character.offsetByCodePoints(Character.java:5372) at CharacterDemo.main(CharacterDemo.java:10)
Example
Now, let us see another scenario where the method throws a NullPointer Exception.
import java.lang.*; public class CharacterDemo { public static void main(String[] args) { char[] c = new char[2]; c = null; int start = 0; int count = 0; int result = Character.offsetByCodePoints(c, start, count, 0, 0); System.out.println("The index within the subarray of c is " + result); } }
Exception
Compile and run the program above, the output will be displayed as −
Exception in thread "main" java.lang.NullPointerExceptionat java.lang.Character.offsetByCodePoints(Character.java:5368)at CharacterDemo.main(CharacterDemo.java:11)