Java StringBuilder codePointAt()示例

Java StringBuilder codePointAt()示例

StringBuilder类的 codePointAt(int index) 方法以一个索引为参数,返回StringBuilder所包含的字符串中该索引处的一个字符的unicode点,或者我们可以说charPointAt()方法返回该索引处字符的 “unicode号码”。索引指的是char值(Unicode代码单位),索引的值必须在0到length-1之间。

如果出现在给定索引上的char值位于高代用范围内,下面的索引小于这个序列的长度,并且下面索引上的char值位于低代用范围内,那么将返回与这个代用对对应的补充码位。否则,将返回给定索引处的char值。

语法

public int codePointAt(int index)

参数: 该方法接受一个int类型的参数 index ,代表要返回unicode值的字符的索引。

返回值: 该方法返回指定位置上的字符的 “unicode号码”。

异常: 当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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程