StringBuffer類的codePointCount()方法用於將BeginBuffer到StringBuffer所包含的String的endIndex指定範圍內的Unicode代碼點數返回。此方法將beginIndex和endIndex作為參數,其中beginIndex是文本範圍的第一個字符的索引,endIndex是文本範圍的最後一個字符的索引。索引引用char值(Unicode代碼單位),並且索引的值必須介於0到length-1之間。範圍從beginIndex開始,在索引endIndex – 1處的字符結束。因此,文本範圍的長度(以字符為單位)為endIndex-beginIndex。
用法:
public int codePointCount(int beginIndex, int endIndex)
參數:此方法有兩個參數:
- beginIndex:int值,表示文本範圍的第一個字符的索引。
- endIndex:int值,表示文本範圍的最後一個字符之後的索引。
返回值:此方法返回int值,該值表示指定文本範圍內的Unicode代碼點的數量。
異常:在以下情況下,此方法將引發IndexOutOfBoundsException:
- beginIndex小於零,
- 或endIndex大於String的長度,
- 或beginIndex大於endIndex。
以下示例程序旨在說明StringBuffer.codePointCount()方法:
示例1:
// Java program to demonstrate
// the codePointCount() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer
str
= new StringBuffer("Welcome to GeeksforGeeks");
// print string
System.out.println("String = " + str.toString());
// returns the codepoint count from index 4 to 10
int codepoints = str.codePointCount(4, 10);
System.out.println("No of Unicode code points "
+ " between index 4 and index 10 = "
+ +codepoints);
}
}
String = Welcome to GeeksforGeeks No of Unicode code points between index 4 and index 10 = 6
示例2:
// Java program to demonstrate
// the codePointCount() 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");
// print string
System.out.println("String = "
+ str.toString());
// returns the codepoint count
// from index 3 to 7
int
codepoints
= str.codePointCount(13, 17);
System.out.println("No of Unicode code points"
+ " between index 13 and 17 = "
+ codepoints);
}
}
String = GeeksForGeeks contribute No of Unicode code points between index 13 and 17 = 4
示例3:演示IndexOutOfBoundsException
// Java program to demonstrate
// exception thrown by the codePointCount() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer
str
= new StringBuffer("GeeksForGeeks");
try {
// make beginIndex greater than endIndex
int codepoints = str.codePointCount(2, 0);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Exception: java.lang.IndexOutOfBoundsException
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#codePointCount(int, int)
相關用法
- Java StringBuilder codePointCount()用法及代碼示例
- Java StringBuffer codePointBefore()用法及代碼示例
- Java StringBuffer substring()用法及代碼示例
- Java StringBuffer toString()用法及代碼示例
- Java StringBuffer replace()用法及代碼示例
- Java StringBuffer setCharAt()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuffer ensureCapacity()用法及代碼示例
- Java StringBuffer codePointAt()用法及代碼示例
- Java StringBuffer lastIndexOf()用法及代碼示例
- Java StringBuffer trimToSize()用法及代碼示例
- Java StringBuffer append()用法及代碼示例
- Java StringBuffer appendCodePoint()用法及代碼示例
- Java StringBuffer delete()用法及代碼示例
- Java StringBuffer reverse()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuffer codePointCount() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。