aspose file tools
The moose likes Beginning Java and the fly likes Is the StringTokenizer class ever better than the split() method of String? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Is the StringTokenizer class ever better than the split() method of String?" Watch "Is the StringTokenizer class ever better than the split() method of String?" New topic
Author

Is the StringTokenizer class ever better than the split() method of String?

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 679

Can the StringTokenizer class do more than the split() method of the String class?

Kaydell
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

No, the opposite, really. Regular expressions -- which didn't exist in Java when StringTokenizer was designed -- are more powerful than the kind of character-based tokenizing that StringTokenizer does.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
There are a few possible advantages to using StringTokenizer, but they're pretty feeble compared to the advanages of split(), I think. One is that StringTokenizer may be simpler to understand for a programmer who doesn't understand regular expressions. At least, if the delimiter includes any special characters like |. But StringTokenizer is still often misunderstood, espeically the fact that it considers only single-character delimiters.

Another feature is that StringTokenizer can return the delimiters as well as the tokens, if you use the appropriate constructor. I don't think I've ever seen a need for this feature, but apparently someone did, once.

And lastly, a StringTokenizer can change delimiters as you go. The split() method definitely can't do that. However, if you have a use for this, then Scanner can do the same thing, and has many more powerful features than StringTokenizer does.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Is the StringTokenizer class ever better than the split() method of String?
 
Similar Threads
I/O: searching an address book text file
Split a String
StringTokenizer not helping in splitting
Efficient text manipulation...
Problem with arrays. StringTokenizers (null pointer)