Java Reference
In-Depth Information
10.11.4 StringTokenizer
The String class provides methods for scanning a string for a particular
character or substring. The class java.util.StringTokenizer allows you
to break a string into substrings, or tokens ,inone operation. (For J2SE 1.4,
much of the value of StringTokenizer has been largely replaced with the
String.split() method, which we discuss in the next section.) The tokens
are separated by delimiters ,which are the characters defined as separators of the
tokens. The default delimiters are the white space characters such as spaces
and line returns. Since StringTokenizer implements the Enumeration
interface, the standard Enumeration methods hasMoreElements() and
nextElement() step through the tokens. Since nextElement() returns an
Object type that requires casting to a String type, StringTokenizer also
includes the nextToken() method that returns a String type automatically.
For naming consistency, there is also a hasMoreTokens() method.
Forexample, if a string contains a sentence, you can use StringTokenizer
to provide a list of the words. For example,
String str ="This is a string object";
StringTokenizer st = new StringTokenizer (str);
while (st.hasMoreTokens ()) {
System.out.println (st.nextToken ());
}
This results in a console output as follows:
This
is
a
string
object
An overloaded constructor allows you to specify the delimiters. For example,
String str =" A*bunch*of*stars " ;
StringTokenizer st = new StringTokenizer (str, " * " );
This breaks the string into the tokens separated by the “* character.
10.11.5 String.split()
J2SE 1.4 added the split() method to the String class to simplify the
task of breaking a string into substrings. This method uses the concept of a
“regular expression” to specify the delimiters. A regular expression is a rem-
nant from the Unix grep tool (“grep” meaning “general regular expression
parser”). A full discussion of regular expressions is beyond the scope of this
topic; see almost any introductory Unix text or the Java API documentation for
Search WWH ::




Custom Search