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


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


StringBuilder類的codePointAt(int index)方法將索引作為參數,並在StringBuilder包含的String中的該索引處返回字符unicode點,或者可以說charPointAt()方法返回該索引處字符的“unicode number”。索引引用char值(Unicode代碼單位),索引的值必須介於0到length-1之間。

如果給定索引處的char值位於high-surrogate範圍內,則下一個索引小於此序列的長度,並且後續索引處的char值位於low-surrogate範圍內,則對應於此代理的補充代碼點對返回。否則,將返回給定索引處的char值。

用法:


public int codePointAt(int index)

參數:此方法接受一個int類型參數索引,該索引表示要返回其unicode值的字符的索引。

返回值:此方法在指定位置返回字符的“unicode number”。

異常:當index為負或大於或等於length()時,此方法將引發IndexOutOfBoundsException。

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

示例1:

// Java program to demonstrate 
// the codePointAt() method 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuilder object 
        StringBuilder str = new StringBuilder(); 
  
        // add the String to StringBuilder Object 
        str.append("Geek"); 
  
        // get unicode of char at position 1 
        int unicode = str.codePointAt(1); 
  
        // print the result 
        System.out.println("StringBuilder Object"
                           + " contains = " + str); 
        System.out.println("Unicode of Character"
                           + " at Position 1 "
                           + "in StringBuilder = "
                           + unicode); 
  
        // get unicode of char at position 3 
        unicode = str.codePointAt(3); 
  
        // print the result 
        System.out.println("Unicode of Character "
                           + "at Position 3 "
                           + "in StringBuilder = "
                           + unicode); 
    } 
}
輸出:
StringBuilder Object contains = Geek
Unicode of Character at Position 1 in StringBuilder = 101
Unicode of Character at Position 3 in StringBuilder = 107

示例2:

// Java program to demonstrate 
// the codePointAt() 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 is " + str.toString()); 
  
        // loop through string and print every Character 
        for (int i = 0; i < str.length(); i++) { 
  
            // get char at position i 
            char ch = str.charAt(i); 
  
            // get unicode of char at position i 
            int unicode = str.codePointAt(i); 
  
            // print char and Unicode 
            System.out.println("Unicode of Char " + ch 
                               + " at position " + i 
                               + " is " + unicode); 
        } 
    } 
}
輸出:
String is WelcomeGeeks
Unicode of Char W at position 0 is 87
Unicode of Char e at position 1 is 101
Unicode of Char l at position 2 is 108
Unicode of Char c at position 3 is 99
Unicode of Char o at position 4 is 111
Unicode of Char m at position 5 is 109
Unicode of Char e at position 6 is 101
Unicode of Char G at position 7 is 71
Unicode of Char e at position 8 is 101
Unicode of Char e at position 9 is 101
Unicode of Char k at position 10 is 107
Unicode of Char s at position 11 is 115

示例3:演示IndexOutOfBoundsException

// Java program demonstrate 
// IndexOutOfBoundsException thrown by 
// the codePointAt() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder 
            str 
            = new StringBuilder("WelcomeGeeks"); 
  
        try { 
  
            // get char at position out of range of index 
            int i = str.codePointAt(str.length()); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Exception: java.lang.StringIndexOutOfBoundsException: 
String index out of range: 12

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



相關用法


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