Package java.util
Class StringTokenizer
- java.lang.Object
-
- java.util.StringTokenizer
-
- All Implemented Interfaces:
Enumeration<Object>
public class StringTokenizer extends Object implements Enumeration<Object>
Breaks a string into tokens; new code should probably useString.split(java.lang.String).// Legacy code: StringTokenizer st = new StringTokenizer("a:b:c", ":"); while (st.hasMoreTokens()) { System.err.println(st.nextToken()); } // New code: for (String token : "a:b:c".split(":")) { System.err.println(token); }- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description StringTokenizer(String string)Constructs a newStringTokenizerfor the parameter string using whitespace as the delimiter.StringTokenizer(String string, String delimiters)Constructs a newStringTokenizerfor the parameter string using the specified delimiters.StringTokenizer(String string, String delimiters, boolean returnDelimiters)Constructs a newStringTokenizerfor the parameter string using the specified delimiters, returning the delimiters as tokens if the parameterreturnDelimitersistrue.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcountTokens()Returns the number of unprocessed tokens remaining in the string.booleanhasMoreElements()Returnstrueif unprocessed tokens remain.booleanhasMoreTokens()Returnstrueif unprocessed tokens remain.ObjectnextElement()Returns the next token in the string as anObject.StringnextToken()Returns the next token in the string as aString.StringnextToken(String delims)Returns the next token in the string as aString.
-
-
-
Constructor Detail
-
StringTokenizer
public StringTokenizer(String string)
Constructs a newStringTokenizerfor the parameter string using whitespace as the delimiter. ThereturnDelimitersflag is set tofalse.- Parameters:
string- the string to be tokenized.
-
StringTokenizer
public StringTokenizer(String string, String delimiters)
Constructs a newStringTokenizerfor the parameter string using the specified delimiters. ThereturnDelimitersflag is set tofalse. Ifdelimitersisnull, this constructor doesn't throw anException, but later calls to some methods might throw aNullPointerException.- Parameters:
string- the string to be tokenized.delimiters- the delimiters to use.
-
StringTokenizer
public StringTokenizer(String string, String delimiters, boolean returnDelimiters)
Constructs a newStringTokenizerfor the parameter string using the specified delimiters, returning the delimiters as tokens if the parameterreturnDelimitersistrue. Ifdelimitersis null this constructor doesn't throw anException, but later calls to some methods might throw aNullPointerException.- Parameters:
string- the string to be tokenized.delimiters- the delimiters to use.returnDelimiters-trueto return each delimiter as a token.
-
-
Method Detail
-
countTokens
public int countTokens()
Returns the number of unprocessed tokens remaining in the string.- Returns:
- number of tokens that can be retreived before an
Exceptionwill result from a call tonextToken().
-
hasMoreElements
public boolean hasMoreElements()
Returnstrueif unprocessed tokens remain. This method is implemented in order to satisfy theEnumerationinterface.- Specified by:
hasMoreElementsin interfaceEnumeration<Object>- Returns:
trueif unprocessed tokens remain.- See Also:
Enumeration.nextElement()
-
hasMoreTokens
public boolean hasMoreTokens()
Returnstrueif unprocessed tokens remain.- Returns:
trueif unprocessed tokens remain.
-
nextElement
public Object nextElement()
Returns the next token in the string as anObject. This method is implemented in order to satisfy theEnumerationinterface.- Specified by:
nextElementin interfaceEnumeration<Object>- Returns:
- next token in the string as an
Object - Throws:
NoSuchElementException- if no tokens remain.- See Also:
Enumeration.hasMoreElements()
-
nextToken
public String nextToken()
Returns the next token in the string as aString.- Returns:
- next token in the string as a
String. - Throws:
NoSuchElementException- if no tokens remain.
-
nextToken
public String nextToken(String delims)
Returns the next token in the string as aString. The delimiters used are changed to the specified delimiters.- Parameters:
delims- the new delimiters to use.- Returns:
- next token in the string as a
String. - Throws:
NoSuchElementException- if no tokens remain.
-
-