import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
/** from
n o w j a v a . c o m**/
public class Main {
public static void main(String[] argv) {
CharacterIterator it = new StringCharacterIterator("abcd");
// Iterate over the characters in the forward direction
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
// Use ch ...
}
// Iterate over the characters in the backward direction
for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) {
// Use ch ...
}
// Other methods
char ch = it.first(); // a
ch = it.current(); // a
ch = it.next(); // b/* 来 自 时 代 J a v a 公 众 号 - N o w J a v a . c o m*/
ch = it.current(); // b
System.out.println(ch);
ch = it.last(); // d
int pos = it.getIndex(); // 3
System.out.println(ch);
ch = it.next(); // DONE
pos = it.getIndex(); // 4
System.out.println(ch);
ch = it.previous(); // d
System.out.println(ch);
ch = it.setIndex(1); // b
// Change the characters
((StringCharacterIterator) it).setText("efgh");
ch = it.current(); // e
// Create an iterator on a substring (efgh)
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。