Test of StringTokenizer
import java.util.*;
public class TokenTest {
public static void main(String[] args) {
StringTokenizer stringT = new StringTokenizer
("We may be through with the past, but" +
" the past ain't through with us.");
while (stringT.hasMoreTokens ( ) ) {
System.out.println(stringT.countTokens() +
" tokens left, next token: " +
stringT.nextToken ( ));
}
}
}
Output:
14 tokens left, next token: We
13 tokens left, next token: may
12 tokens left, next token: be
11 tokens left, next token: through
10 tokens left, next token: with
9 tokens left, next token: the
8 tokens left, next token: past,
7 tokens left, next token: but
6 tokens left, next token: the
5 tokens left, next token: past
4 tokens left, next token: ain't
3 tokens left, next token: through
2 tokens left, next token: with
1 tokens left, next token: us.
import java.util.*;
public class TokenTest2 {
public static void main(String[] args) {
StringTokenizer stringT = new StringTokenizer
("We may be through with the past, but" +
" the past ain't through with us.",
" ,.\n\t"); // note second argument, delimiters
while (stringT.hasMoreTokens ( ) ) {
System.out.println(stringT.countTokens() +
" tokens left, next token: " +
stringT.nextToken ( ));
}
}
}
Output:
14 tokens left, next token: We
13 tokens left, next token: may
12 tokens left, next token: be
11 tokens left, next token: through
10 tokens left, next token: with
9 tokens left, next token: the
8 tokens left, next token: past
7 tokens left, next token: but
6 tokens left, next token: the
5 tokens left, next token: past
4 tokens left, next token: ain't
3 tokens left, next token: through
2 tokens left, next token: with
1 tokens left, next token: us