Java - Character codePointCount() Method



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.

Example

The following example shows the usage of Java Character codePointCount(CharSequence seq, int beginIndex, int endIndex) method.

package com.tutorialspoint;
import java.lang.*;
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

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.

import java.lang.*;
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

Example

The following example shows the usage of Java Character codePointCount(char[] a, int offset, int count) method.

package com.tutorialspoint;
import java.lang.*;
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

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;
import java.lang.*;
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)

Example

The method also throws a NullPointer Exception when the character sequence argument is null.

package com.tutorialspoint;
import java.lang.*;
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)
java_lang_character.htm
Advertisements