String codePointCount() method in java

Let’s learn String codePointCount(int beginIndex, int endIndex) method java.

String codePointCount(int beginIndex, int endIndex) method java

String codePointCount(int beginIndex, int endIndex) method returns the number of Unicode code points in the specified text range.

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.

Syntax:

public int codePointCount(int beginIndex, int endIndex)

Parameters:

beginIndex the index to the first char of the text range.
endIndex the index after the last char of the text range.

Throws:

IndexOutOfBoundsException – if the beginIndex is negative, or endIndex is larger than the length of this String, or beginIndex is larger than endIndex.

Example

If the given beginIndex and endIndex value in String codePointCount(int beginIndex, int endIndex) method is positive and less than given string length then codePointCount(int beginIndex, int endIndex) method returns the number of Unicode code points in the specified text range.

class StringCodePointCountMethodDemo 
{
   public static void main(String[] args) 
   {
      String str = new String("JavaProgramming");
      System.out.println("Given string : " + str);
      //initialize the beginIndex and endIndex value
      int beginIndex = 3, endIndex = 6;
      System.out.println("Given beginIndex and endIndex values are: " + beginIndex + " and " + endIndex);
      System.out.println("Count of character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: " + str.codePointCount(beginIndex, endIndex));
   }
}

Output:

Given string : JavaProgramming
Given beginIndex and endIndex values are: 3 and 6
Count of character code point at the given range (3,6) is: 3


If the given beginIndex and endIndex value in String codePointCount(int beginIndex, int endIndex) method is zero then codePoint(int beginIndex, int endIndex) returns zero.

class StringCodePointCountMethodDemo 
{
   public static void main(String[] args) 
   {
      String str = new String("JavaProgramming");
      System.out.println("Given string : " + str);
      //initialize the beginIndex and endIndex value
      int beginIndex = 0, endIndex = 0;
      System.out.println("Given beginIndex and endIndex values are: " + beginIndex + " and " + endIndex);
      System.out.println("Count of character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: " + str.codePointCount(beginIndex, endIndex));
   }
}

Output:

Given string : JavaProgramming
Given beginIndex and endIndex values are: 0 and 0
Count of character code point at the given range (0,0) is: 0


If the given beginIndex value is greater than endIndex value in String codePointCount(int beginIndex, int endIndex) method then this method throws IndexOutOfBoundException.

class StringCodePointCountMethodDemo 
{
   public static void main(String[] args) 
   {
      try 
      {
         String str = "HelloWorld";
         System.out.println("Given string is : " + str);
         int beginIndex = 8, endIndex = 4;
         System.out.println("Given beginIndex and endIndex values are: " + beginIndex + " and " + endIndex);
         System.out.println("Count of character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: " + str.codePointCount(beginIndex, endIndex));
      } 
      catch (Exception ex) 
      {
         ex.printStackTrace();
         System.out.println("Exception is : " + ex);
      }
   }
}

Output:

Given string is : HelloWorld
java.lang.IndexOutOfBoundsException: Range [8, 4) out of bounds for length 10
at java.base/java.lang.String.codePointCount(String.java:1618)
at ArraysInJava/com.Strings.java.StringCodePointCountMethodDemo.main(StringCodePointCountMethodDemo.java:13)
Exception is : java.lang.IndexOutOfBoundsException


Also read – while loop in java