Java中的 StringBuilder codePointAt() 示例
StringBuilder 类的codePointAt(int index)方法将索引作为参数,并返回 StringBuilder 包含的 String 中该索引处的字符unicode 点,或者我们可以说 charPointAt() 方法返回该索引处字符的“unicode 编号” . index 指的是 char 值(Unicode 代码单元),index 的值必须介于 0 到 length-1 之间。
如果给定索引处的 char 值位于高代理范围内,则后续索引小于此序列的长度,并且后续索引处的 char 值位于低代理范围内,则补充代码点返回对应于这个代理对。否则,返回给定索引处的 char 值。
句法:
public int codePointAt(int index)
参数:该方法接受一个 int 类型参数index ,它表示要返回其 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 Java)