java.lang.Character codePointCount(CharSequence seq, int beginIndex, int endIndex)

Description

The Character.codePointCount(CharSequence seq, int beginIndex, int endIndex) java 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. Thus the length (in chars) of the text range is endIndex-beginIndex. Unpaired surrogates within the text range count as one code point each.

Make a note that the codePointCount method 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(CharSequence seq, int beginIndex, int endIndex) method throws the following exception:

  • NullPointerException – if seq is null.
  • IndexOutOfBoundsException – if the beginIndex is negative, or endIndex is larger than the length of the given sequence, or beginIndex is larger than endIndex.

Method Syntax

public static int codePointCount(CharSequence seq, int beginIndex, int endIndex)

Method Argument

Data Type Parameter Description
CharSequence seq the char array
int beginIndex the index to the first char of the text range.
int endIndex the index after the last char of the text range.

Method Returns

The codePointCount(CharSequence seq, int beginIndex, int endIndex) method of Character class returns the number of Unicode code points in the specified text range.

Compatibility

Requires Java 1.5 and up

Java Character codePointCount(CharSequence seq, int beginIndex, int endIndex) Example

Below is a simple java example on the usage of codePointCount(CharSequence seq, int beginIndex, int endIndex) method of Character class.

package com.javatutorialhq.java.examples;


/*
 * This example source code demonstrates the use of 
 * codePointCount(CharSequence seq, int beginIndex, int endIndex)
 * method of Character class.
 */

public class CharacterCodePointCountCharSequenceExample {

	public static void main(String[] args) {

		// initialize a new CharSequence object
		CharSequence cs = "This is a test string!";
		
		// initialize beginIndex and endIndex
		int beginIndex = 0;
		int endIndex = cs.length();
		
		// get the number of Unicode code points in the text 
		// range of the specified char sequence		
		int result = Character.codePointCount(cs, beginIndex,endIndex);
		
		System.out.println("Result:"+result);
	}

}

Sample Output

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

java Character codePointCount(CharSequence seq, int beginIndex, int endIndex) example output