|
public class StringTokenizerextends Objectimplements Enumeration <Object> The string tokenizer class allows an application to break strings into tokens. The tokenization method is simpler than the methods used by the StreamTokenizer class. The StringTokenizer method does not distinguish between identifiers, numbers, and quoted strings, nor do they recognize and skip comments.
You can specify it at creation time, or you can specify a set of separators (characters that separate the tags) based on each tag.
An instance of StringTokenizer behaves in two ways, depending on whether the value of the returnDelims flag it uses when it is created is true or false:
If the flag is false, the delimiter character is used to separate the tags. A token is the largest sequence of consecutive characters (not delimiters).
If the flag is true, those separator characters are considered tokens themselves. So the token is either a delimiter character or the largest sequence of consecutive characters (not delimiters).
The StringTokenizer object internally maintains the current position to be marked in the string. Some operations move this current position after the processed character.
Returns a token by intercepting a substring of a string that is used to create a StringTokenizer object.
Here is an example using a tokenizer. code show as below:
StringTokenizer st = new StringTokenizer ("this is a test");
while (st.hasMoreTokens ()) {
System.out.println (st.nextToken ());
}
The following string is output:
this
is
a
test
StringTokenizer is a legacy class that is retained for compatibility reasons (although its use is discouraged in new code). It is recommended that anyone seeking this feature use the split method of String or the java.util.regex package.
The following example illustrates how to use the String.split method to split a string into basic tokens:
String [] result = "this is a test" .split ("\\s");
for (int x = 0; x <result.length; x ++)
System.out.println (result [x]);
The following string is output:
this
is
a
test |
|