Signup/Sign In
LAST UPDATED: OCTOBER 16, 2020

Java Character codePointCount(char[]a, int offset, int count) Method

Java codePointCount() method is a part of Character class. This method returns the Unicode codepoint count of the sub-array of the specified char array. The index of the first char in the specified char array is the specified offset and the length of the sub-array is the specified count.

Syntax:

public static int codePointCount(char[] a, int offset, int count)  

Parameters:

The parameter passed are:

char array,

int offset value which is the first char of the specified char array

int count which denoted the length of the sub-array.

Returns:

Returns the number of Unicode code point in the specified sub-array.

Example 1:

Here, the total number of Unicode code point in the subarray is returned by using the codePointCount() method.

import java.lang.Character;

public class StudyTonight
{    
   public static void main(String [] args)throws Exception
   {  
     char[] ch1 = new char[] { 'm', 'o', 'h', 'i', 't' };  
     int offset1  = 0, count1 = 3;  
     int r1 = Character.codePointCount(ch1, offset1, count1);  
     System.out.println("The number unicode points is " + r1);  
     
     char[] ch2 = new char[] { '1', '2', '3', '4', '5' };  
     int offset2  = 1, count2 = 3;  
     int r2 = Character.codePointCount(ch2, offset2, count2);  
     System.out.println("The number of Unicode points is " + r2);   
   }
}


The number of Unicode code point for alphabets is 3
The number of Unicode code point for numbers is 3

Example 2:

Here, the number of Unicode code points in the subarray of the String is returned.

import java.lang.Character;

public class StudyTonight
{    
   public static void main(String [] args)throws Exception
   {  
     String s1 = "Welcome to StudyTonight";   
     System.out.println("Welcome to StudyTonight");  
     int cp1 = s1.codePointCount(2, 12);  
     System.out.println(cp1 + " is the total count of unicode code point");  
     
     String s2 = "Java is fun";  
     System.out.println("Java is fun");  
     int cp2 = s2.codePointCount(4, 9);  
     System.out.println(cp2 + " is the total count of unicode code point");  
   }
}


Welcome to StudyTonight
10 is the total count of unicode code point
Java is fun
5 is the total count of unicode code point

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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.