目录

int codePointCount(int beginIndex, int endIndex)

描述 (Description)

java.lang.StringBuilder.codePointCount()方法返回此序列的指定文本范围内的Unicode代码点数。

文本范围从指定的beginIndex开始,并扩展到索引endIndex - 1处的char。 因此,文本范围的长度(以字符为单位)是endIndex - beginIndex

声明 (Declaration)

以下是java.lang.StringBuilder.codePointCount()方法的声明

public int codePointCount(int beginIndex, int endIndex)

参数 (Parameters)

  • beginIndex - 这是文本范围的第一个char的索引。

  • endIndex - 这是文本范围的最后一个字符之后的索引。

返回值 (Return Value)

此方法返回指定文本范围内的Unicode代码点数。

异常 (Exception)

IndexOutOfBoundsException - 如果beginIndex为负数,或者endIndex大于此序列的长度,或者beginIndex大于endIndex。

例子 (Example)

以下示例显示了java.lang.StringBuilder.codePointCount()方法的用法。

package com.iowiki;
import java.lang.*;
public class StringBuilderDemo {
   public static void main(String[] args) {
      StringBuilder str = new StringBuilder("programming");
      System.out.println("string = " + str);
      // returns the codepoint count from index 1 to 5
      int retval = str.codePointCount(1, 5);
      System.out.println("Count = " + retval);
      str = new StringBuilder("amrood admin ");
      System.out.println("string = " + str);
      // returns the codepoint count from index 3 to 9
      retval = str.codePointCount(3, 9);
      System.out.println("Count = " + retval);
   }
}  

让我们编译并运行上面的程序,这将产生以下结果 -

string = programming
Count = 4
string = amrood admin
Count = 6
↑回到顶部↑
WIKI教程 @2018