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.
8 comments:
Nice one.
Thanks for that...
thanks .. you are more than welcome .. comments keep me going !!
Thanks. Nice one
Thanks a lot.
thank you , your comments get me going
The reason why split is slow is that it builds the pattern every time.
vijay
thanks 4 d update viay
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?
Post a Comment