Thursday, May 8, 2008

String.split( ) VS stringTokenizer

tring.split() was introduced in jdk 1.4'

For one thing, StringTokenizer returns one substring at a time whereas the split method returns an array of substrings.
Best to use String's split(...) method. The StringTokenizer class is a legacy class (NOT deprecated)

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,

StringTokenizer can split strings based on single characters, split() takes regular expressions.
I imagine that might make split() a bit slower when splitting for single characters (can't be arsed to test it
though), but usually it's more handy.


Meghala Devi said...

Nice one.
Thanks for that...

Lavnish said...

thanks .. you are more than welcome .. comments keep me going !!

vijay said...

Thanks. Nice one

Anonymous said...

Thanks a lot.

Lavnish said...

thank you , your comments get me going

vijay kumar said...

The reason why split is slow is that it builds the pattern every time.

Lavnish said...

thanks 4 d update viay

Microbiology said...

You showed some good code snippets, in my project i need to input the string and make it to parts.Can i use Split() method instead of Tokernizer?