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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。