Java Reference
In-Depth Information
StrTokDemo.java
StringTokenizer st
=
new
new
StringTokenizer
(
"Hello World of Java"
);
while
while
(
st
.
hasMoreTokens
( ))
System
.
out
.
println
(
"Token: "
+
st
.
nextToken
( ));
ors or Enumerations for Data-Independent Access
)
, but if you use the methods thereof you
need to cast the results to
String
.
A
StringTokenizer
normally breaks the
String
into tokens at what we would think of as
“word boundaries” in European languages. Sometimes you want to break at some other char-
acter. No problem. When you construct your
StringTokenizer
, in addition to passing in the
string to be tokenized, pass in a second string that lists the “break characters.” For example:
StrTokDemo2.java
StringTokenizer st
=
new
new
StringTokenizer
(
"Hello, World|of|Java"
,
", |"
);
while
while
(
st
.
hasMoreElements
( ))
System
.
out
.
println
(
"Token: "
+
st
.
nextElement
( ));
It outputs the four words, each on a line by itself, with no punctuation.
But wait, there's more! What if you are reading lines like:
FirstName|LastName|Company|PhoneNumber
and your dear old Aunt Begonia hasn't been employed for the last 38 years? Her “Company”
ample, you'll see that it has two delimiters together (the comma and the space), but if you
run it, there are no “extra” tokens—that is, the
StringTokenizer
normally discards adjacent
consecutive delimiters. For cases like the phone list, where you need to preserve null fields,
there is good news and bad news. The good news is that you can do it: you simply add a
second argument of
true
when constructing the
StringTokenizer
, meaning that you wish
to see the delimiters as tokens. The bad news is that you now get to see the delimiters as
tokens, so you have to do the arithmetic yourself. Want to see it? Run this program: