http://www.blogjava.net/default.aspx?id=-10&cateid=3891
一個完整的Unicode字符叫代碼點/CodePoint,而一個Java char 叫代碼單元code unit;
string對象以UTF-16保存Unicode字符,需要用2個字符表示一個超大字符集漢字,這種表示方式爲
Sruuogate,第一個字符叫Surrogate High,第二個就是Surrogate Low
判斷一個char是否是Surrogate區的字符,用Character的isHighSurrogate()/isLowSurrogate()方法。
從兩個Surrogate High/Low字符,返回一個完整的Unicode CodePoint用Character.toCodePoint()/codePointAt()
一個Code Point,可能需要一個也可能需要兩個char表示,因此不能直接使用CharSequence.length()
方法返回一個字符串到底有多少個漢字,而需要用String.codePointCount()/Character.codePointCount()
要定位字符串中的第N個字符,不能直接將n作爲偏移量,而需要從字符串頭部依次遍歷得到,需要
String.offsetByCodePoints()
從字符串的當前字符,找到上一個字符,不能直接用offset實現,而需要
String.codePointBefore(),或String.offsetByCodePoints()
從當前字符,找下一個字符,需要判斷當前CodePoint的長度,再計算得到
String.offsetByCodePoints()
看了之後還是不太明白
String str("web開發");
int len = str.length();
System.out.println();
輸出結果爲5;
改爲用int len = str.codePointCount(0,str.length());
輸出結果同樣爲5
定位一個字符
用char cp = str.charAt(4);
輸出爲“發”;
若用int index = str.offsetByCodePoints(0,4);
int cp = str.codePointAt(index);
輸出爲21457;什麼意思??原來是獲取字符串中指定位置的字符的UNICODE值,其值等同於(int)charAt(i)
jdk1.5中,string類新方法介紹:
http://www.javayou.com/html/diary/showlog.vm?sid=2&log_id=557