「Java」BreakIteratorクラスでサロゲートペア文字数をカウントする
構文
1.java.text.BreakIterator
BreakIteratorクラスは、テキスト内の境界の位置を見つけるメソッドを実装します。
BreakIteratorのインスタンスは現在の位置を維持し、テキストをスキャンして境界が発生する文字のインデックスを返します。
BreakIteratorクラスは、テキスト内の境界の位置を見つけるメソッドを実装します。
BreakIteratorのインスタンスは現在の位置を維持し、テキストをスキャンして境界が発生する文字のインデックスを返します。
BreakIteratorクラスは、テキスト内の境界の位置を見つけるメソッドを実装します。 BreakIteratorのインスタンスは現在の位置を維持し、テキストをスキャンして境界が発生する文字のインデックスを返します。
2.public void setText(String newText)
スキャンされる新しいテキスト文字列を設定します。
現在のスキャン位置はfirst()にリセットされます。
スキャンされる新しいテキスト文字列を設定します。
現在のスキャン位置はfirst()にリセットされます。
スキャンされる新しいテキスト文字列を設定します。 現在のスキャン位置はfirst()にリセットされます。
3.public static final int DONE
DONEは、最初または最後のテキスト境界に到達したときに、
previous()、next()、next(int)、preceding(int)、およびfollowing(int)によって返されます。
使用例
package com.arkgame.study;
import java.text.BreakIterator;
public class SurrogateDemo {
public static void main(String[] args) {
String str = "?????????";
BreakIterator bit = BreakIterator.getCharacterInstance();
bit.setText(str);
int cnt = 0;
while (bit.next() != BreakIterator.DONE) {
cnt++;
}
System.out.println("実行結果\n文字数:" + cnt);
}
}
package com.arkgame.study;
import java.text.BreakIterator;
public class SurrogateDemo {
public static void main(String[] args) {
String str = "?????????";
BreakIterator bit = BreakIterator.getCharacterInstance();
bit.setText(str);
int cnt = 0;
while (bit.next() != BreakIterator.DONE) {
cnt++;
}
System.out.println("実行結果\n文字数:" + cnt);
}
}
package com.arkgame.study; import java.text.BreakIterator; public class SurrogateDemo { public static void main(String[] args) { String str = "?????????"; BreakIterator bit = BreakIterator.getCharacterInstance(); bit.setText(str); int cnt = 0; while (bit.next() != BreakIterator.DONE) { cnt++; } System.out.println("実行結果\n文字数:" + cnt); } }
実行結果
文字数:9
文字数:9
文字数:9