Contents | Prev | Next | Index

21.10 The Class java.util.StringTokenizer

The StringTokenizer class provides a way to break a String into tokens. The tokenizing method used by this class is much simpler than the one used by the class java.io.StreamTokenizer. For example, a StringTokenizer does not distinguish among identifiers, numbers, and quoted strings; moreover, it does not recognize and skip comments.

A StringTokenizer can serve as an Enumeration (§21.1).

public class StringTokenizer implements Enumeration {
	public StringTokenizer(String str, String delim,
		boolean returnTokens);
	public StringTokenizer(String str, String delim);
	public StringTokenizer(String str);
	public boolean hasMoreTokens();
	public String nextToken();
	public String nextToken(String delim);
	public boolean hasMoreElements();
	public Object nextElement();
	public int countTokens();
}
A StringTokenizer simply divides characters into classes: delimiters and other characters. The tokenizer behaves in one of two ways, depending on whether it was created with returnTokens having the value true or false.

If returnTokens is false, delimiter characters merely serve to separate tokens of interest. A token is thus a maximal sequence of consecutive characters that are not delimiters.

If returnTokens is true, delimiter characters are themselves considered to be tokens of interest. A token is thus either one delimiter character or a maximal sequence of consecutive characters that are not delimiters.

A StringTokenizer internally maintains a current position within the String to be tokenized. Some operations advance this current position past the characters processed.

A token is returned by taking a substring (§20.12.32) of the string that was used to create the StringTokenizer.

21.10.1 public StringTokenizer(String str, String delim,
boolean returnTokens)

This constructor initializes a newly created StringTokenizer so that it will recognize tokens within the given string str. All characters in the string delim will be considered delimiters. The argument returnTokens specifies whether delimiter characters themselves are to be considered tokens.

21.10.2 public StringTokenizer(String str, String delim)

This constructor initializes a newly created StringTokenizer so that it will recognize tokens within the given string str. All characters in the string delim will be considered delimiters. Delimiter characters themselves will not be treated as tokens.

21.10.3 public StringTokenizer(String str)

This constructor initializes a newly created StringTokenizer so that it will recognize tokens within the given string str. All whitespace characters (§20.5.19) will be considered delimiters. Delimiter characters themselves will not be treated as tokens.

21.10.4 public boolean hasMoreTokens()

The result is true if and only if there is at least one token in the string after the current position. If this method returns true, then a subsequent call to nextToken with no argument will successfully return a token.

21.10.5 public String nextToken()

The next token in the string after the current position is returned. The current position is advanced beyond the recognized token.

21.10.6 public String nextToken(String delim)

First, the set of characters considered to be delimiters by this StringTokenizer is changed to be the characters in the string delim. Then the next token in the string after the current position is returned. The current position is advanced beyond the recognized token.

21.10.7 public boolean hasMoreElements()

This method has exactly the same behavior as hasMoreTokens (§21.10.4). It is provided so that a StringTokenizer can serve as an Enumeration (§21.1).

21.10.8 public Object nextElement()

This method has exactly the same behavior as nextToken (§21.10.5). It is provided so that a StringTokenizer can serve as an Enumeration (§21.1).

21.10.9 public int countTokens()

The result is the number of tokens in the string after the current position, using the current set of delimiter characters. The current position is not advanced.


Contents | Prev | Next | Index

Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com



Spec-Zone.ru - all specs in one place



free hit counter