LAST UPDATED: OCTOBER 16, 2020
Java Character codePointCount(CharSequence seq, int beginIndex, int endIndex) Method
Java codePointCount()
method is a part of Character
class. This method returns the Unicode codepoint count in the text range which begins from the specified beginIndex and ends at the specified endIndex.
Syntax:
public static int codePointCount(CharSequence seq, int beginIndex, int endIndex)
Parameters:
The parameters passed are:
Returns:
Returns the number of Unicode code points in the specified text range.
Example 1:
Here, the Unicode codepoint count in the specified text range is returned.
import java.lang.Character;
public class StudyTonight
{
public static void main(String [] args)throws Exception
{
CharSequence cs = "Learning Java is fun";
System.out.println("Learning Java is fun");
int begin = 0;
int end = cs.length();
int r = Character.codePointCount(cs, begin,end);
System.out.println("The count is : "+r);
}
}
Learning Java is fun
The count is : 20
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the desired output.
import java.lang.Character;
import java.util.Scanner;
public class StudyTonight
{
public static void main(String [] args)throws Exception
{
try
{
System.out.println("Enter charSequence");
Scanner sc = new Scanner(System.in);
CharSequence s = sc.nextLine();
System.out.println("Enter begin index");
int n1 = sc.nextInt();
System.out.println("Enter end index");
int n2 = sc.nextInt();
System.out.println("The codepoint count is : " +Character.codePointCount(s,n1,n2));
}
catch(Exception e)
{
System.out.println("Invalid Input");
}
}
}
Enter charSequence
welcome to java
Enter begin index
2
Enter end index
6
The codepoint count is : 4
**********************************
Enter charSequence
take care
Enter begin index
10
Enter end index
6
Invalid Input
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.