📜  Java中的 StringBuffer codePointCount() 方法及示例

📅  最后修改于: 2022-05-13 01:55:07.604000             🧑  作者: Mango

Java中的 StringBuffer codePointCount() 方法及示例

StringBuffer 类codePointCount()方法用于返回StringBuffer 包含的String 的beginIndex 到endIndex 指定范围内的Unicode 码点数。该方法以 beginIndex 和 endIndex 作为参数,其中 beginIndex 是文本范围的第一个字符的索引,endIndex 是文本范围的最后一个字符之后的索引。索引指的是 char 值(Unicode 代码单元),并且 index 的值必须介于 0 到 length-1 之间。范围从 beginIndex 开始,在索引 endIndex – 1 处的字符结束。因此,文本范围的长度(以字符为单位)为 endIndex-beginIndex。

句法:

public int codePointCount(int beginIndex,
                               int endIndex)

参数:此方法有两个参数:

  • beginIndex : int 值,表示文本范围的第一个字符的索引。
  • endIndex : int 值,表示文本范围最后一个字符之后的索引。

返回值:此方法返回表示指定文本范围内 Unicode 代码点数的int 值

异常:如果出现以下情况,此方法将引发IndexOutOfBoundsException

  • beginIndex 小于零,
  • 或 endIndex 大于 String 的长度,
  • 或 beginIndex 大于 endIndex。

下面的程序说明了 StringBuffer.codePointCount() 方法:

示例 1:

// Java program to demonstrate
// the codePointCount() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer
            str
            = new StringBuffer("Welcome to GeeksforGeeks");
  
        // print string
        System.out.println("String = " + str.toString());
  
        // returns the codepoint count from index 4 to 10
        int codepoints = str.codePointCount(4, 10);
  
        System.out.println("No of Unicode code points "
                           + " between index 4 and index 10 = "
                           + +codepoints);
    }
}
输出:
String = Welcome to GeeksforGeeks
No of Unicode code points  between index 4 and index 10 = 6

示例 2:

// Java program to demonstrate
// the codePointCount() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer
            str
            = new StringBuffer("GeeksForGeeks contribute");
  
        // print string
        System.out.println("String = "
                           + str.toString());
  
        // returns the codepoint count
        // from index 3 to 7
        int
            codepoints
            = str.codePointCount(13, 17);
  
        System.out.println("No of Unicode code points"
                           + " between index 13 and 17 = "
                           + codepoints);
    }
}
输出:
String = GeeksForGeeks contribute
No of Unicode code points between index 13 and 17 = 4

示例 3:演示 IndexOutOfBoundsException

// Java program to demonstrate
// exception thrown by the codePointCount() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer
            str
            = new StringBuffer("GeeksForGeeks");
  
        try {
  
            // make beginIndex greater than endIndex
            int codepoints = str.codePointCount(2, 0);
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Exception: java.lang.IndexOutOfBoundsException

参考:
https://docs.oracle.com/javase/10/docs/api/java Java, int)