Java Character offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) Method
Java offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset)
method is a part of Character
class. This method returns the index within the given char
subarray that is offset from the given index
by codePointOffset
code points. The start
and count
arguments specify a subarray of the char
array. Unpaired surrogates within the text range given by index
and codePointOffset
count as one code point each.
Syntax:
public static int offsetByCodePoints(char[]a, int start, int count, int index, int codePointOffset)
Parameters:
The parameters passed are:
- a
char
array
start
- the index of the first char
of the subarray
count
- the length of the subarray in char
s
index
- the index to be offset
codePointOffset
- the offset in code points
Returns:
Returns the index within the specified subarray.
Exceptions:
NullPointerException
- if the char array is null.
Example 1:
Here the index within the specified subarray of the char array is returned.
public class StudyTonight
{
public static void main(String[] args)
{
char[] ch = new char[] { 'm', 'o', 'h', 'i', 't' };
int start = 1;
int count = 4;
int r = Character.offsetByCodePoints(ch, start, count, 1, 3);
System.out.println("The index within the specified range is : " + r);
}
}
The index within the specified range is : 4
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the characters in array: ");
char[] ch = sc.next().toCharArray();
System.out.print("Enter start,count,index and codePointOffset");
int start = sc.nextInt();
int count = sc.nextInt();
int index = sc.nextInt();
int offset = sc.nextInt();
int r = Character.offsetByCodePoints(ch, start, count, index, offset);
System.out.println("The index within the specified range is : " + r);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
Enter the characters in array: studytonight
Enter start,count,index and codePointOffset 1 5 1 2
The index within the specified range is : 3
*************************************************************
Enter the characters in array: hello
Enter start,count,index and codePointOffset 1 2 1 3
Invalid Input!!
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.