C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
HasMoreTokens: This returns true if the StringTokenizer contains more tokens. The delimiter is known from the last call to nextToken.
NextToken: This receives a String. Any of the characters in the string are considered possible delimiters. It returns the next token.
While: This loop will continue iterating infinitely, until the hasMoreTokens() method returns false.
WhileJava program that uses StringTokenizer
import java.util.StringTokenizer;
public class Program {
public static void main(String[] args) {
// Create tokenizer with specified String.
StringTokenizer tokenizer = new StringTokenizer("123,cat 456");
// Loop over all tokens separated by comma or space.
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken(", "));
}
}
}
Output
123
cat
456
Also: Regular expression patterns, and groups, may be used instead of split and the StringTokenizer class.
RegexJava program that uses split
public class Program {
public static void main(String[] args) {
final String value = "123,cat 456";
// Tokenize the String with a regular expression in Split.
String[] tokens = value.split("[,\\ ]");
for (String token : tokens) {
System.out.println(token);
}
}
}
Output
123
cat
456