- 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 codePointCount() Method
Description
The Java Character codePointCount() method is used to calculate the number of Unicode code points in the text range of the specific char sequence.
The text range begins certain starting index and extends to the char at the index before the ending index. Thus, the length (in chars) of the text range is ending index − starting index.
Note − Unpaired surrogates within the text range count as one code point each.
Syntax
Following is the syntax for Java Character codePointCount() method
public static int codePointCount(CharSequence seq, int beginIndex, int endIndex) or, public static int codePointCount(char[] a, int offset, int count)
Parameters
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
a − The char array
offset − The index to the first char of the char array
count − The length of the subarray in chars
Return Value
This method returns the number of Unicode code points in the specified text range or a subarray.
Getting Code Point Count in a CharSequence using given Indexes Example
The following example shows the usage of Java Character codePointCount(CharSequence seq, int beginIndex, int endIndex) method. When we pass a CharSequence and index values as the arguments to the codePointCount(CharSequence seq, int beginIndex, int endIndex) method, the return value is the count of unicode character present at given range in the given CharSequence.
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create a CharSequence seq and assign value CharSequence seq = "Hello World!"; // create and assign value to bi, ei int bi = 4, ei = 8; // create an int res int count; // assign result of codePointCount on seq to res using bi, ei count = Character.codePointCount(seq, bi, ei); String str = "No. of Unicode code points is " + count; // print count value System.out.println( str ); } }
Output
Let us compile and run the above program, this will produce the following result −
No. of Unicode code points is 4
Getting Code Point Count in a alphabetical CharSequence using given Indexes Example
Instead of an alphabetical character sequence, let us pass a digital character sequence as an argument to this method. The return value will be the number of digits present in the text range.
public class CharacterDemo { public static void main(String args[]) { CharSequence ch = "0123456789"; int result = Character.codePointCount(ch, 3, 7); System.out.println("The unicode code point count in given character sequence is " + result); } }
Output
We must compile and run the given program above to obtain the output as follows −
The unicode code point count in given character sequence is 4
Getting Code Point Count in a CharSequence using given Indexes Example
The following example shows the usage of Java Character codePointCount(char[] a, int offset, int count) method. When we pass a char[] and index values as the arguments to the codePointCount(char[] a, int offset, int count) method, the return value is the count of unicode character present at given range in the given char[].
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create a char a[] and assign value char a[] = {'t', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's'}; // create and assign value to offset, count int offset = 2, count = 5; // create an int res int res; // assign result of codePointCount on a[] to res using offset, count res = Character.codePointCount(a, offset, count); // print count value System.out.println("No. of Unicode code points is " + res); } }
Output
If we compile and run the above program, the following result is displayed −
No. of Unicode code points is 5
Facing Exception while Getting Code Point Count in a CharSequence at invalid Index Example
However, this method throws an IndexOutOfBounds Exception when the ending index is greater than the length of the character sequence argument.
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // assign value to seq CharSequence seq = "Hello World!"; // create and assign value to bi, ei such that the program throws an exception int bi = 4, ei = 18; // create an int res int res; // call the codePointCount method result = Character.codePointCount(seq, bi, ei); // print res value System.out.println("No. of Unicode code points is " + res); } }
Exception
On compiling and running the program above, the exception is thrown as follows −
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.lang.Character.codePointCount(Character.java:5225) at com.tutorialspoint.CharacterDemo.main(CharacterDemo.java:19)
Facing Exception while Getting Code Point Count in a null CharSequence at given Index Example
But if the CharSequence is initialized with a null value and passed as an argument to the method, a NullPointer Exception is raised.
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create a CharSequence seq and assign value CharSequence seq = null; // create and assign value to bi, ei int bi = 4, ei = 8; // create an int res int res; // assign result of codePointCount on seq to res using bi, ei res = Character.codePointCount(seq, bi, ei); String str = "No. of Unicode code points is " + res; // print res value System.out.println( str ); } }
Exception
The program throws a NullPointer Exception instead of dislaying the output as follows −
Exception in thread "main" java.lang.NullPointerException at java.lang.Character.codePointCount(Character.java:5223) at com.tutorialspoint.CharacterDemo.main(CharacterDemo.java:19)