Java/Development Class/CharacterIterator
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 Change the characters
- 2 Create an iterator on a substring (efgh)
- 3 Iterate a subset of a string
- 4 Iterate each characters of a string
- 5 Iterate over the characters in the backward direction
- 6 Iterating the Characters of a String
- 7 Reverse a string using CharacterIterator
- 8 Use CharacterIterator to loop through a string
Change the characters
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class Main {
public static void main(String[] argv) throws Exception {
CharacterIterator it = new StringCharacterIterator("abcd");
((StringCharacterIterator) it).setText("efgh");
char ch = it.current();
}
}
Create an iterator on a substring (efgh)
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class Main {
public static void main(String[] argv) throws Exception {
CharacterIterator it = new StringCharacterIterator("abcd");
int begin = 5;
int end = 9;
int pos = 6;
it = new StringCharacterIterator("abcd efgh ijkl", begin, end, pos);
char ch = it.current();
System.out.println(ch);
ch = it.last();
System.out.println(ch);
}
}
Iterate a subset of a string
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class Main {
public static void main(String[] args) {
String text = "this is a test";
CharacterIterator it = new StringCharacterIterator(text, 4, 27, 5);
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
System.out.print(ch);
}
}
}
Iterate each characters of a string
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class Main {
public static void main(String[] args) {
String text = "The quick brown fox jumps over the lazy dog";
CharacterIterator it = new StringCharacterIterator(text);
int vowels = 0;
int consonants = 0;
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
if (ch == "a" || ch == "e" || ch == "i" || ch == "o" || ch == "u") {
vowels = vowels + 1;
} else if (ch != " ") {
consonants = consonants + 1;
}
}
System.out.println("Number of vowels: " + vowels);
System.out.println("Number of consonants: " + consonants);
}
}
Iterate over the characters in the backward direction
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class Main {
public static void main(String[] argv) throws Exception {
CharacterIterator it = new StringCharacterIterator("abcd");
for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) {
System.out.println(ch);
}
}
}
Iterating the Characters of a String
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class Main {
public static void main(String[] argv) throws Exception {
CharacterIterator it = new StringCharacterIterator("abcd");
// Iterate over the characters in the forward direction
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
System.out.println(ch);
}
}
}
Reverse a string using CharacterIterator
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class Main {
private static final String text = "this is a test";
public static void main(String[] args) {
CharacterIterator it = new StringCharacterIterator(text);
for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) {
System.out.print(ch);
}
}
}
Use CharacterIterator to loop through a string
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class Main {
public static void main(String[] argv) throws Exception {
CharacterIterator it = new StringCharacterIterator("abcd");
char ch = it.first();
ch = it.current();
ch = it.next();
ch = it.current();
ch = it.last();
int pos = it.getIndex();
ch = it.next();
pos = it.getIndex();
ch = it.previous();
ch = it.setIndex(1);
}
}