Java Character.codePointCount()方法

原文:https://www.studytonight.com/java-wrapper-class/java-character-codepointcount-char-int-int-method

Java codePointCount()方法是Character类的一部分。此方法返回指定字符数组的子数组的 Unicode 代码点计数。指定字符数组中第一个字符的索引是指定的偏移量,子数组的长度是指定的计数。

语法:

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

参数:

传递参数有:

char数组,

int指定字符数组的第一个字符的偏移值

int表示子数组长度的计数。

返回:

返回指定子数组中的 Unicode 码点数。

例 1:

这里使用codePointCount()方法返回子数组中 Unicode 码点的总数。

import java.lang.Character;

public class StudyTonight
{    
   public static void main(String [] args)throws Exception
   {  
     char[] ch1 = new char[] { 'm', 'o', 'h', 'i', 't' };  
     int offset1  = 0, count1 = 3;  
     int r1 = Character.codePointCount(ch1, offset1, count1);  
     System.out.println("The number unicode points is " + r1);  

     char[] ch2 = new char[] { '1', '2', '3', '4', '5' };  
     int offset2  = 1, count2 = 3;  
     int r2 = Character.codePointCount(ch2, offset2, count2);  
     System.out.println("The number of Unicode points is " + r2);   
   }
}

字母的 Unicode 码点数为 3 数字的 Unicode 码点数为 3

例 2:

这里,返回字符串的子数组中的 Unicode 码点数。

import java.lang.Character;

public class StudyTonight
{    
   public static void main(String [] args)throws Exception
   {  
     String s1 = "Welcome to StudyTonight";   
     System.out.println("Welcome to StudyTonight");  
     int cp1 = s1.codePointCount(2, 12);  
     System.out.println(cp1 + " is the total count of unicode code point");  

     String s2 = "Java is fun";  
     System.out.println("Java is fun");  
     int cp2 = s2.codePointCount(4, 9);  
     System.out.println(cp2 + " is the total count of unicode code point");  
   }
}

欢迎学习今晚 10 是 unicode 码点总数 Java 很好玩 5 是 unicode 码点总数

实时示例:

在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。