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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。