Character.codePointCount(): This method is available in java.lang.Character class of Java.
Syntax:
int java.lang.Character.codePointCount(CharSequence seq, int beginIndex, int endIndex)
This method takes three arguments, one of type CharSequence and the other two are of type int as its parameters. This method returns the number of Unicode code points in the text range of the specified char sequence. The text range begins at the specified beginIndex and extends to the char at index endIndex - 1.
Parameters: Three parameters are required for this method.
seq: the char sequence.
beginIndex: the index to the first char of the text range.
endIndex: the index after the last char of the text range.
Returns: the number of Unicode code points in the specified text range.
Throws:
1. NullPointerException - if seq is null.
2. IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of the given sequence, or beginIndex is larger than endIndex.
Approach 1: When no exceptions.
Java
public class CharactercodePointCountCharSequence {public static void main(String[] args) {CharSequence seq = "Hello";int beginIndex = 0, endIndex = 3;System.out.println(Character.codePointCount(seq, beginIndex, endIndex));}}
Output:
3
Approach 2: NullPointerException
Java
public class CharactercodePointCountCharSequence {public static void main(String[] args) {CharSequence seq = null;int beginIndex = 0, endIndex = 3;System.out.println(Character.codePointCount(seq, beginIndex, endIndex));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.length()" because "seq" is null at java.base/java.lang.Character.codePointCount(Character.java:9209)
Approach 3: IndexOutOfBoundsException
Java
public class CharactercodePointCountCharSequence {public static void main(String[] args) {CharSequence seq = "Hello";int beginIndex = -1, endIndex = 3;System.out.println(Character.codePointCount(seq, beginIndex, endIndex));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.lang.Character.codePointCount(Character.java:9211)
No comments:
Post a Comment