Java.lang.Character.codePointCount() Method

Advertisements


Description

The java.lang.Character.codePointCount(char[ ] a, int offset, int count) 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.

Declaration

Following is the declaration for java.lang.Character.codePointCount() method

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

Parameters

  • a - the char array

  • offset - the index of the first char in the given char array

  • count - the length of the subarray in chars

Return Value

This method returns the number of Unicode code points in the specified subarray.

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.

Example

The following example shows the usage of lang.Character.codePointCount() method.

package com.tutorialspoint;

import java.lang.*;

public class CharacterDemo {

   public static void main(String[] args) {

      // create a char array c and assign values
      char[] c = new char[] { 'a', 'b', 'c', 'd', 'e' };

      // create and assign value to offset, count
      int offset  = 1, count = 3;

      // create an int res
      int res;

      // assign result of codePointCount on subarray of c to res
      res = Character.codePointCount(c, offset, count);

      String str = "No. of Unicode code points is " + res;

      // print res value
      System.out.println( str );
   }
}

Let us compile and run the above program, this will produce the following result:

No. of Unicode code points is 3



Advertisements
Advertisements