Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

StringTokenizer versus String.split()

807569Sep 28 2006 — edited Sep 28 2006
I just discovered that the Stringtokenizer class exists, but I do not know when I should use it over the String class's split() method to parse a string into separate values.

Could someone explain the when to use one over the other?

Thank You,

John Gooch

Comments

807569
For one thing, StringTokenizer returns one substring at a time whereas the split method returns an array of substrings.
800282
> I just discovered that the Stringtokenizer class
exists, but I do not know when I should use it over
the String class's split() method to parse a string
into separate values.

Could someone explain the when to use one over the
other?

Thank You,

John Gooch

Best to use String's split(...) method. The StringTokenizer class is deprecated which means that it might not appear in future releases of Sun's JDK/JRE (won't happen soon though, if at all).

From Java's StringTokenizer API-docs:
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 functionality use the split method of String or the java.util.regex package instead.

See: http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html
807569
I tried to edit the message, but you guys replied quickly ( mucho appreciated! ).

Here is the edited version ( your answer have already helped ) :
I just discovered that the Stringtokenizer class exists, but I do not know when I should use it over the String class's split() method to parse a string into separate values.

Could someone explain the when to use one over the other?

The reason I am looking at this is that a I have a properties files with the entry called "exclusions" that lists directories that are excluded from global file purging rules because they have their own rules.

So an entry would be formatted like
<path>,<maximum file age in hours>,<recurse>

Concrete Example:
/root,24,0
/tmp,48,1


So the root directory would have files older than 24 hours deleted, but the purging process would not enter subdirectories to look for files to purge there.

The /tmp folder would have files older than 48 hours deleted, and the process would also check ( recurse ) subdirectories for files to delete.

I am deciding on the best way to store these values in the application, because as it recurses the directory structure, it needs to check each directory to see if it is excluded from the default settings ( which may be 24 hours max age, and recursion set to true ( 1 ) .



Thank You,

John Gooch
807569
> The StringTokenizer class is deprecated...

In 1.6? I haven't seen that version yet, and it's not deprecated in 1.5. I'd be surprised if actually gets deprecated due to its widespread usage, but I've been surprised before...

~
807569
> The StringTokenizer class is deprecated...

In 1.6? I haven't seen that version yet, and it's not
deprecated in 1.5. I'd be surprised if actually gets
deprecated due to its widespread usage, but I've been
surprised before...

~
In the API docs for 1.5, just above the Constructor Summary. It even includes an example of how the String.split method can be used.
800282
> Could someone explain the when to use one over the
other?

Sun advises to use String's split(...) method over the StringTokenizer class in new code. So that split(...) method can do (at least) as much as the StringTokenizer can.
So unless you have specific reasons * to use the StringTokenizer, I'd use the String's split(...).

* running JDK 1.3 (or before)
807569
In 1.6? I haven't seen that version yet, and it's not deprecated in 1.5. I'd be
surprised if actually gets deprecated due to its widespread usage, but I've
been surprised before...
It's not deprecated according to http://download.java.net/jdk6/docs/api/java/util/StringTokenizer.html.

StringTokenizer is an order of magnitude quicker than String.split() for the simplest operations that don't involve regex so for those to whom every microsecond counts, it can be legitimately used!
807569
> In the API docs for 1.5, just above the Constructor
Summary. It even includes an example of how the
String.split method can be used.

Where does it say it's deprecated? I understand it says its use is discouraged in new code (I've posted it before many a time). But the term deprecated carries specific implications, and I don't see where it says StringTokenizer has been deprecated.

~
800282
> In 1.6? I haven't seen that version yet, and it's not
deprecated in 1.5. I'd be surprised if actually gets
deprecated due to its widespread usage, but I've been
surprised before...

~

Hmmm, I had another look and the API says "StringTokenizer is a legacy class".
I must've interpreted it the wrong way; so it's not deprecated?
807569
> Hmmm, I had another look and the API says
"StringTokenizer is a legacy class".
I must've interpreted it the wrong way; so it's not
deprecated?

Nothing I've seen indicates it is; if it were, I would think it would say so...

~
800282
> Where does it say it's [i]deprecated? I
understand it says its use is discouraged in new code
(I've posted it before many a time). But the term
deprecated carries specific implications, and
I don't see where it says StringTokenizer has been
deprecated.

~
Ah, ok thanks for the info.

@OP: sorry for my (partially) incorrect answer!
800282
... if it were, I would think it would say so...

~
I blame my English teacher from high school for my mistake!
; )
807569
It even includes an example of how
the String.split method can be used.
An incorrect example, by the way. By default, StringTokenizer uses whitespace to delimit tokens, and if there are two or more delimiter characters in a row, it collapses them into one delimiter. So the split() equivalent would be
  String[] result = "this is a test".split("\\s+");
But if you really want to treat each character as a separate delimiter, you just remove the plus sign again. This is, in my opinion, the best reason to choose split(); while there is a way to do single-character delimiters in StringTokenizer, it's kludgy and awkward.

And yes, StringTokenizer is inherently faster, but "orders of magnitude" is a gross overstatement. You're very unlikely to notice the speed difference in your real-world programs.
1 - 13
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 26 2006
Added on Sep 28 2006
13 comments
395 views