Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why StringTokenizer Discouraged nowadays ?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

String split is taken advantage over StringTokenizer . please anyone can explain me the reason?
 
naresh voota
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String Tokenizer does not accept(correction) regular expressions but split does. I believe that is why it is not encouraged these days.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, but still you can manage with delimiter . i guess some other reason would be there
 
Sultan Khadar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Venkatasamy,

If you want to tokenize a String Object then go for split instead of StringTokenizer
If the data is outside from your program (From a File or from a DataSource) and if you want to parse it then prefer Scanner

Hope this will clarify your doubt.

Regards,
Sultan
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you don't need a regex to match what you're doing? What if it's just Strings separated by commas like so:

MARY, JIM, JOHN, ZACH

Is string split still easier to use than StringTokenizer for this ???

Hunter.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easier to use is a matter of opinion, not fact. Do either of these look easier to use or understand than the other?



 
naresh voota
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringTokenizer is faster than String.split() but does not offer functionality like splitting basing on regex expression. But when you look at real world applications, the speed offered by StringTokenizer compared to String.split does not matter much. Sun is keeping StringTokenizer as it is used in legacy applications and for backward compatibility. Hence the advice to switch to String.split for newer code. While the case of String and Scanner is different. It is more of a case of different horses for different courses.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More reasons:
1) What if your requirement changes from comma to comma with optional whitespace? It's much easier to change the code using split.
2) split is an idiom that reads more naturally
3) It's harder to misuse split. I've seen a delimiter of ", ". Upon first read, many people think this means matching a comma followed by a space. It doesn't of course, which wastes time in troubleshooting.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Folks for your earnest reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic