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 = GeekUnicode of Character at Position 1 in StringBuilder = 101Unicode 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 WelcomeGeeksUnicode of Char W at position 0 is 87Unicode of Char e at position 1 is 101Unicode of Char l at position 2 is 108Unicode of Char c at position 3 is 99Unicode of Char o at position 4 is 111Unicode of Char m at position 5 is 109Unicode of Char e at position 6 is 101Unicode of Char G at position 7 is 71Unicode of Char e at position 8 is 101Unicode of Char e at position 9 is 101Unicode of Char k at position 10 is 107Unicode 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)
