StringBuffer類的codePointAt()方法按StringBuffer包含的順序在該索引處返回一個字符Unicode點。此方法返回該索引處字符的“Unicodenumber”。索引值必須介於0到length-1之間。
如果給定索引處的char值位於high-surrogate範圍內,則下一個索引小於此序列的長度,並且後續索引處的char值位於low-surrogate範圍內,則對應於此代理的補充代碼點對返回。否則,將返回給定索引處的char值。
用法:
public int codePointAt(int index)
參數:此方法采用一個參數索引,該參數索引是int值,表示要返回其unicode值的字符的索引。
返回值:此方法返回指定索引處字符的unicode號。
異常:當index為負或大於或等於length()時,此方法將引發IndexOutOfBoundsException。
下麵的程序演示StringBuffer類的codePointAt()方法:
示例1:
// Java program to demonstrate
// the codePointAt() method
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
StringBuffer str = new StringBuffer();
// add the String to StringBuffer Object
str.append("Geeksforgeeks");
// get unicode of char at position 10
int unicode = str.codePointAt(10);
// print the result
System.out.println("Unicode of Character "
+ "at Position 10 "
+ "in StringBuffer = "
+ unicode);
}
}
輸出:
Unicode of Character at Position 10 in StringBuffer = 101
示例2:演示IndexOutOfBoundsException
// Java program demonstrate
// IndexOutOfBoundsException thrown by
// the codePointAt() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer
str
= new StringBuffer("GeeksForGeeks Contribute");
try {
// get char at position 25 which is
// greater then length
int i = str.codePointAt(25);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception: " + e);
}
}
}
輸出:
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 25
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#codePointAt(int)
相關用法
- Java StringBuilder codePointAt()用法及代碼示例
- Java StringBuffer codePointBefore()用法及代碼示例
- Java StringBuffer trimToSize()用法及代碼示例
- Java StringBuffer append()用法及代碼示例
- Java StringBuffer toString()用法及代碼示例
- Java StringBuffer setCharAt()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuffer offsetByCodePoints()用法及代碼示例
- Java StringBuffer ensureCapacity()用法及代碼示例
- Java StringBuffer indexOf()用法及代碼示例
- Java StringBuffer codePointCount()用法及代碼示例
- Java StringBuffer reverse()用法及代碼示例
- Java StringBuffer appendCodePoint()用法及代碼示例
- Java StringBuffer delete()用法及代碼示例
- Java StringBuffer deleteCharAt()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuffer codePointAt() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。