StringBuilder类的codePointBefore()方法返回StringBuilder包含的String中指定文本范围内的Unicode代码点数。此方法采用两个索引作为参数:first beginIndex表示文本范围的第一个字符的索引,endIndex表示文本范围的最后一个字符的索引。索引引用char值(Unicode代码单位),并且索引的值必须介于0到length-1之间。文本范围从beginIndex开始,并扩展到索引endIndex – 1处的char。因此,文本范围的长度(以char为单位)为endIndex-beginIndex。
用法:
public int codePointCount(int beginIndex, int endIndex)
参数:该方法接受两个参数
- beginIndex:文本范围的第一个字符的索引。
- endIndex:文本范围的最后一个字符之后的索引。
返回值:此方法返回指定文本范围内的Unicode代码点数。
异常:在以下情况下,此方法将引发IndexOutOfBoundsException:
- beginIndex小于零,
- 或endIndex大于String的长度,
- 或beginIndex大于endIndex。
下面的程序演示StringBuilder类的codePointCount()方法:
示例1:
// Java program to demonstrate
// the codePointCount() 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 = " + str.toString());
// returns the codepoint count from index 2 to 8
int codepoints = str.codePointCount(2, 8);
System.out.println("No of Unicode code points = "
+ codepoints);
}
}
输出:
String = WelcomeGeeks No of Unicode code points = 6
示例2:
// Java program to demonstrate
// the codePointCount() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("GeeksForGeeks");
// print string
System.out.println("String = "
+ str.toString());
// returns the codepoint count
// from index 3 to 7
int
codepoints
= str.codePointCount(3, 7);
System.out.println("No of Unicode code points"
+ " between index 3 and 7 = "
+ codepoints);
}
}
输出:
String = GeeksForGeeks No of Unicode code points between index 3 and 7 = 4
示例3:演示IndexOutOfBoundsException
// Java program to demonstrate
// exception thrown by the codePointCount() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("GeeksForGeeks");
try {
// make beginIndex greater than endIndex
int codepoints = str.codePointCount(7, 4);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
Exception: java.lang.IndexOutOfBoundsException
参考:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#codePointCount(int, int)
相关用法
- Java StringBuffer codePointCount()用法及代码示例
- Java StringBuilder deleteCharAt()用法及代码示例
- Java StringBuilder subSequence()用法及代码示例
- Java StringBuilder ensureCapacity()用法及代码示例
- Java StringBuilder setLength()用法及代码示例
- Java StringBuilder codePointAt()用法及代码示例
- Java StringBuilder capacity()用法及代码示例
- Java StringBuilder codePointBefore()用法及代码示例
- Java StringBuilder charAt()用法及代码示例
- Java StringBuilder delete()用法及代码示例
- Java StringBuilder replace()用法及代码示例
- Java StringBuilder setCharAt()用法及代码示例
- Java StringBuilder reverse()用法及代码示例
- Java StringBuilder length()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 StringBuilder codePointCount() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。