Character.offsetByCodePoints(): This method is available in java.lang.Character class of Java.
Syntax:
int java.lang.Character.offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset)
This method takes five arguments one of type char array and the other is of type int as its parameters. 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.
Parameters: Five parameters are required for this method.
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.
codePointOffset: the offset in code points.
Returns: the index within the subarray.
Throws:
1. NullPointerException - if a is null.
2. IndexOutOfBoundsException - if start or count is negative, or if start + count is larger than the length of the given array, or if the index is less than start or larger then start +count, or if codePointOffset is positive and the text range starting with index and ending with start + count - 1 has fewer than codePointOffset codepoints, or if codePointOffset is negative and the text range starting with start and ending with index - 1 has fewer than the absolute value of codePointOffset code points.
Approach 1: When no exceptions.
Java
public class CharacteroffsetByCodePointschararray {public static void main(String[] args) {char[] a = { 'H', 'e', 'l', 'l', 'o', 'J', 'a', 'v', 'a' };int start = 0, count = 2, index = 2;int codePonitOffset = 0;System.out.println(Character.offsetByCodePoints(a,start, count, index, codePonitOffset));}}
Output:
2
Approach 2: NullPointerException
Java
public class CharacteroffsetByCodePointschararray {public static void main(String[] args) {char[] a = null;int start = 0, count = 2, index = 2;int codePonitOffset = 0;System.out.println(Character.offsetByCodePoints(a, start, count, index, codePonitOffset));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "a" is null at java.base/java.lang.Character.offsetByCodePoints(Character.java:9354)
Approach 3: IndexOutOfBoundsException
Java
public class CharacteroffsetByCodePointschararray {public static void main(String[] args) {char[] a = { 'H', 'e', 'l', 'l', 'o', 'J', 'a', 'v', 'a' };int start = 0, count = 2, index = 2;int codePonitOffset = 5;System.out.println(Character.offsetByCodePoints(a, start, count, index, codePonitOffset));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.lang.Character.offsetByCodePointsImpl(Character.java:9374) at java.base/java.lang.Character.offsetByCodePoints(Character.java:9358)
No comments:
Post a Comment