当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Character codePointCount()用法及代码示例


此方法有两种类型的语法。

Java 字符 codePointCount(char[]a, int offset, int count) 方法

codePointCount(char[]a, int offset, int count) 方法用于返回char数组的子数组中Unicode码点的个数。 offset 参数是给定 char 数组中第一个 char 的索引,count 参数表示子数组的长度。

用法

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

参数

上述方法需要三个参数:

  • 字符数组。
  • 偏移量是给定字符数组中第一个字符的索引。
  • 计数是给定字符中子数组的长度。

返回值

此方法返回指定子数组中 Unicode 代码点的数量。

例子1

public class CharactercodePointCountMethodExample1 {
	public static void main(String[] args) {
		System.out.println("Welcome to our tutorial site:");
		CharSequence seq = "Hello";  
		// Declare offset.
		int offset = 2;
		// Declare count.
		int countValue = 4;		
		int result = Character.codePointCount(seq, offset, countValue);
	     // Print the result.
		System.out.println("The result is given as:" + result);
	
       }
}

输出:

Welcome to our tutorial site:
The result is given as:2

例子2

public class CharactercodePointCountMethodExample2 {
    public static void main(String[] args) {
      System.out.println("The alphabets are:'a', 'b', 'c', 'd', 'e'");
	 char[] c1 = new char[] { 'a', 'b', 'c', 'd', 'e' };
	 int offset1  = 0, count1 = 4;
	 int res1 = Character.codePointCount(c1, offset1, count1);
System.out.println("The number of Unicode code point for alphabets are " + res1);
System.out.println("The numbers are: '1', '2', '3', '4', '5'");
	 char[] c2 = new char[] { '1', '2', '3', '4', '5' };
	 int offset2  = 1, count2 = 3;
	 int res2 = Character.codePointCount(c2, offset2, count2);
System.out.println("The number of Unicode code point for numbers are " + res2);
	   }
	}

输出:

The alphabets are:'a', 'b', 'c', 'd', 'e'
The number of Unicode code point for alphabets are 4
The numbers are: '1', '2', '3', '4', '5'
The number of Unicode code point for numbers are 3

例子3

public class CharactercodePointCountMethodExample3{
   public static void main(String args[])
    {
     String str1 = "Hello everyone"; 
     System.out.println("Hello everyone");
     int Total1 = str1.codePointCount(3, 9);
     System.out.println(Total1 + " is generated because method counts the first argument.");
     String str2 = "Everday is a fun day";
     System.out.println("Everyday is a fun day");
     int Total2 = str2.codePointCount(6, 8);
     System.out.println(Total2 + " is generated because method counts the second argument.");
     }
  }

输出:

Hello everyone
6 is generated because method counts the first argument.
Everyday is a fun day
2 is generated because method counts the second argument.

Java 字符 codePointCount()Method

Character 类的 codePointCount(CharSequence seq, int beginIndex, int endIndex) 方法用于返回特定字符序列的文本范围内的 Unicode 代码点数。文本范围从 beginIndex 开始,到 endIndex 结束。

用法

Public static int codePointCount(CharSequence seq, int beginIndex, int endIndex)

参数

上述方法需要三个参数:

  • 字符序列。
  • beginIndex 是文本范围的第一个字符的索引。
  • endIndex 是文本范围最后一个字符之后的索引。

返回值

codePointCount(CharSequence seq, int beginIndex, int endIndex) 方法返回指定文本范围内的 Unicode 代码点数。

示例 4

public class CharactercodePointCountMethodExample4 {
	public static void main(String[] args) {
       // Initialize a new CharSequence object
		CharSequence cs = "This is a unique example!";
	    System.out.println("This is a unique example!");
	// Initialize beginIndex and endIndex
		int beginIndex = 0;
		int endIndex = cs.length();	
		int result = Character.codePointCount(cs, beginIndex,endIndex);
	   System.out.println("The result comes to be:"+result);
	}
}

输出:

This is a unique example!
The result comes to be:25

例 5

public class CharactercodePointCountMethodExample5 {
     public static void main(String[] args) { // Initialize a new CharSequence object
	CharSequence cs1 = "This is a unique example!"; System.out.println("This is a unique example!"); 
	// Initialize beginIndex and endIndex
	int beginIndex1 = 0;
	int endIndex1 = cs1.length();	
	int result1 = Character.codePointCount(cs1, beginIndex1,endIndex1);
	System.out.println("The result comes to be:"+result1);
	CharSequence cs2 = "Hello World!";
	System.out.println(cs2);
       int beginIndex2 = 4, endIndex2 = 8;
       int result2 = Character.codePointCount(cs2, beginIndex2, endIndex2);
       String str = "Number of Unicode code points is " + result2;
      System.out.println( str );
        }
}

输出:

This is a unique example!
The result comes to be:25
Hello World!
Number of Unicode code points is 4

例 6

public class CharactercodePointCountMethodExample6 {
	public static void main(String[] args) {

		// Initialize a new CharSequence object
		CharSequence cs1 = "This is a unique example!";
		System.out.println(cs1);
		// Initialize beginIndex and endIndex
		int beginIndex1 = 0;
		int endIndex1 = cs1.length();	
		int result1 = Character.codePointCount(cs1, beginIndex1,endIndex1);
		System.out.println("The result comes to be:"+result1);
	    CharSequence cs2 = "Java is a high level language!";
		System.out.println(cs2);
		int beginIndex2 = 0;
		int endIndex2 = cs2.length();	
		int result2 = Character.codePointCount(cs2, beginIndex2,endIndex2);
		System.out.println("The result comes to be:"+result2);
		CharSequence cs3 = "Java is based on the cocepts of OOPS!";
		System.out.println(cs3);
		int beginIndex3 = 0;
		int endIndex3 = cs3.length();	
		int result3 = Character.codePointCount(cs3, beginIndex3,endIndex3);
		System.out.println("The result comes to be:"+result3);
	
	}
		
	}

输出:

This is a unique example!
The result comes to be:25
Java is a high level language!
The result comes to be:30
Java is based on the cocepts of OOPS!
The result comes to be:37



相关用法


注:本文由纯净天空筛选整理自 Java Character codePointCount() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。