
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java String codePointCount() Method
The Java String codePointCount() method is used to return the number(count) of Unicode code points in the specified text range of this String. The text range begins at the specified beginIndex and extends to the char at index endIndex - 1. Therefore, the length (in chars) of the text range is endIndex-beginIndex.
The indexes are referring the character position in the string, and the first char value denotes the 0th index, and the second char value denotes the 1st index, and so on. The codePointCount() method accept two parameters as an integer that holds the beginIndex and endIndex value.
Syntax
Following is the syntax of the Java String codePointCount() method −
public int codePointCount(int beginIndex, int endIndex)
Parameters
beginIndex − This is the index to the first char of the text range.
endIndex − This is the index after the last char of the text range.
Return Value
This method returns the number of Unicode code points in the specified text range.
Example
If the given beginIndex and endIndex value are zero, the codePointcount() method return zero.
In the following program, we are creating an object of the string class with the value “HelloWorld”. Then, using the codePointCount() method, we are trying to count the character code point value at the given range (beginIndex = 0, endIndex = 0).
package com.tutorialspoint.String; import java.lang.*; public class Demo { public static void main(String[] args) { //create an object of the string class String str = new String("HelloWorld"); System.out.println("The given string is: " + str); //initialize the beginIndex and endIndex value int beginIndex = 0; int endIndex = 0; System.out.println("The given beginIndex and endIndex values are: " + beginIndex + " and " + endIndex); //using codePointCount() method System.out.println("The count of character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: " + str.codePointCount(beginIndex, endIndex)); } }
Output
On executing the above program, it will produce the following result −
The given string is: HelloWorld The given beginIndex and endIndex values are: 0 and 0 The count of character code point at the given range (0,0) is: 0
Example
If the given beginIndex value is greater than the endIndex value, this method throws IndexOutOfBoundException.
In this example, we are creating a string literal with the value “JavaProgramming”. Then, using the codePointCount() method, we are trying to print the count of the character code point at the given range, since the beginIndex value is greater than the endIndex the method throws an exception.
import java.lang.*; public class Demo { public static void main(String[] args) { try { //string initialization String str = "Welcome to Tutorials Point"; System.out.println("The string value is: " + str); // initialize the beginIndex and endIndex value int beginIndex = 10, endIndex = 5; System.out.println("The beginIndex and endIndex values are: " + beginIndex + " and " + endIndex); //using the codePointCount() method System.out.println("The count of character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: " + str.codePointCount(beginIndex, endIndex)); } catch (Exception e) { e.printStackTrace(); System.out.println("The exception is: " + e); } } }
Output
Following is the output of the above program −
The string value is: Welcome to Tutorials Pointjava.lang.IndexOutOfBoundsException at java.base/java.lang.String.codePointCount(String.java:1610) at TP.main(TP.java:16) The exception is: java.lang.IndexOutOfBoundsException
Example
If the given beginIndex and endIndex value are positive and less than the string length, the codePointCount () method return the count of the character code point at the given range.
In the following example, we are instantiating the string class with the value “TutorialsPoint”. Then, using the codePointCount() method, we are trying to count the character code point value at the given range ( beginIndex = 2, endIndex = 6).
package com.tutorialspoint.String; import java.lang.*; public class Test { public static void main (String [] args) { // instantiate the String class String str = new String("TutorialsPoint"); System.out.println("The given string is: " + str); //initialize the index values int beginIndex = 2, endIndex = 6; System.out.println("The beginIndex and endIndex values are: " + beginIndex + " and " + endIndex); System.out.print( "The count of the character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: "+ str.codePointCount(beginIndex, endIndex)); } }
Output
The above program, produces the following results −
The given string is: TutorialsPoint The beginIndex and endIndex values are: 2 and 6 The count of the character code point at the given range (2,6) is: 4