To use StringTokenizer, you specify an ascribe cord and a cord that contains delimiters. Delimiters are characters that abstracted tokens. Each appearance in the delimiters cord is advised a accurate delimiter—for example, ",;:" sets the delimiters to a comma, semicolon, and colon. The absence set of delimiters consists of the whitespace characters: space, tab, newline, and carrying return.
The StringTokenizer constructors are apparent here:
StringTokenizer(String str)
StringTokenizer(String str, String delimiters)
StringTokenizer(String str, String delimiters, boolean delimAsToken)
In all versions, str is the cord that will be tokenized. In the aboriginal version, the absence delimiters are used. In the additional and third versions, delimiters is a cord that specifies the delimiters. In the third version, if delimAsToken is true, again the delimiters are aswell alternate as tokens if the cord is parsed. Otherwise, the delimiters are not returned.
Delimiters are not alternate as tokens by the aboriginal two forms. Once you accept created a StringTokenizer object, the nextToken( ) adjustment is acclimated to abstract after tokens. The hasMoreTokens( ) adjustment allotment accurate while there are added tokens to be extracted. Since StringTokenizer accouterments Enumeration, the hasMoreElements( ) and nextElement( ) methods are aswell implemented, and they act the aforementioned as hasMoreTokens( ) and nextToken( ), respectively.
Here is an archetype that creates a StringTokenizer to anatomize "key=value" pairs. After sets of "key=value" pairs are afar by a semicolon.
// Demonstrate StringTokenizer.
import java.util.StringTokenizer;
class STDemo {
static String in = "title=Java example;" +
"author=krozbonek;" +
"publisher=alifewhatever.blogspot.com;" +
"copyright=2011;";
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer(in, "=;");
while(st.hasMoreTokens()) {
String key = st.nextToken();
String val = st.nextToken();
System.out.println(key + "\t" + val);
}
}
}
The output is shown :
title Java example
author krozbonek
publisher alifewhatever.blogspot.com
copyright 2011
No comments:
Post a Comment