java.lang.Character codePointCount(char[] a, int offset, int count)

Description

The Character.codePointCount(char[] a, int offset, int count) java method returns the number of Unicode code points in a subarray of the char array argument. The offset argument is the index of the first char of the subarray and the count argument specifies the length of the subarray in chars. Unpaired surrogates within the subarray count as one code point each.

Make a note that the codePointCountmethod of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:

Character.codePointCount(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java codePointCount() method non statically.

Notes:

The codePointCount(char[] a, int offset, int count) method throws the following exception:

  • NullPointerException – if a is null.
  • IndexOutOfBoundsException – if offset or count is negative, or if offset + count is larger than the length of the given array.

Method Syntax

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

Method Argument

Data Type Parameter Description
char[] a the char array
int index the index following the code point that should be returned

Method Returns

The public static int codePointCount(char[] a, int offset, int count) method of Character class returns the number of Unicode code points in the specified subarray.

Compatibility

Requires Java 1.5 and up

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

Below is a simple java example on the usage of codePointCount(char[] a, int offset, int count) method of Character class.

package com.javatutorialhq.java.examples;

/*
 * This example source code demonstrates the use of 
 * codePointCount(char[] a, int offset, int count)
 * method of Character class.
 */

public class CharacterCodePointCountCharArrayExample {

	public static void main(String[] args) {
		String s = "This is a test string!";

		// initialize a char array object
		char[] cs = s.toCharArray(); // convert string to char array

		// declare offset
		int offset = 0;

		// declare count
		int countValue = cs.length;

		/*
		 * get the number of Unicode code points in a 
		 * subarray of the char array argument
		 */		
		int result = Character.codePointCount(cs, offset, countValue);

		// print the result
		System.out.println("Result:" + result);
	}

}

Sample Output

Below is the sample output when you run the above example.

java Character codePointCount(char[] a, int offset, int count) example output