當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java StringBuilder codePointCount()用法及代碼示例


StringBuilder類的codePointBefore()方法返回StringBuilder包含的String中指定文本範圍內的Unicode代碼點數。此方法采用兩個索引作為參數:first beginIndex表示文本範圍的第一個字符的索引,endIndex表示文本範圍的最後一個字符的索引。索引引用char值(Unicode代碼單位),並且索引的值必須介於0到length-1之間。文本範圍從beginIndex開始,並擴展到索引endIndex – 1處的char。因此,文本範圍的長度(以char為單位)為endIndex-beginIndex。

用法:

public int 
    codePointCount(int beginIndex, int endIndex)

參數:該方法接受兩個參數


  • beginIndex:文本範圍的第一個字符的索引。
  • endIndex:文本範圍的最後一個字符之後的索引。

返回值:此方法返回指定文本範圍內的Unicode代碼點數。

異常:在以下情況下,此方法將引發IndexOutOfBoundsException:

  • beginIndex小於零,
  • 或endIndex大於String的長度,
  • 或beginIndex大於endIndex。

下麵的程序演示StringBuilder類的codePointCount()方法:

示例1:

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

示例2:

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

示例3:演示IndexOutOfBoundsException

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

參考:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#codePointCount(int, int)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuilder codePointCount() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。