java.lang.Character.codePointCount(CharSequence seq, int beginIndex, int endIndex)方法實例
java.lang.Character.codePointCount(CharSequence seq, int beginIndex, int endIndex) 返回Unicode代碼點指定字符序列的文本範圍的數量。
文本範圍始於指定的beginIndex和擴展到字符索引endIndex-1的文本範圍。這樣的長度(以字符)是endIndex- beginIndex。在文本範圍內未配對的代理算作每一個代碼點。
聲明
以下是**java.lang.Character.codePointCount()**方法的聲明
public static int codePointCount(CharSequence seq, int beginIndex, int endIndex)
參數
seq - 該字符序列
beginIndex - 索引的文本範圍的第一個字符
endIndex - 文本範圍的最後一個字符的索引
返回值
該方法返回指定的文本範圍的Unicode代碼點的數量。
異常
NullPointerException - 如果seq爲null。
IndexOutOfBoundsException - 如果beginIndex是負數,或endIndex是比給定序列的長度大,或beginIndex是大於endIndex。
例子
下面的例子顯示lang.Character.codePointCount()方法的使用。
package com.yiibai; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create a CharSequence seq and assign value CharSequence seq = "Hello World!"; // create and assign value to bi, ei int bi = 4, ei = 8; // create an int res int res; // assign result of codePointCount on seq to res using bi, ei res = Character.codePointCount(seq, bi, ei); String str = "No. of Unicode code points is " + res; // print res value System.out.println( str ); } }
讓我們來編譯和運行上面的程序,這將產生以下結果:
No. of Unicode code points is 4