Write a program to count a number of Unicode code points in the specified text range of a String


This Java program demonstrates how to use the codePointCount() method of the String class to count the number of Unicode code points in a substring of a given string.

The codePointCount() method takes two arguments, beginIndex and endIndex, which specify the range of the substring whose code points are to be counted. The method returns the number of Unicode code points in the specified substring.

In this program, the string "Tutor Joes" is assigned to the str variable. The codePointCount() method is called with arguments 1 and 10, which specify the range of the substring "utor Joe". The number of code points in this substring is stored in the variable c.

Finally, the System.out.println() method is used to print the original string and the count of code points in the specified substring.

Source Code

public class CodePoint_Count
{
	public static void main(String[] args)
	{
		String str = "Tutor Joes";
		System.out.println("Given String : " + str);
		int c = str.codePointCount(1, 10);
		System.out.println("Codepoint count : " + c);
	}
}

Output

Given String : Tutor Joes
Codepoint count : 9

Example Programs