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

Easier to use is a matter of opinion, not fact. Do either of these look easier to use or understand than the other?

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

naresh voota

Greenhorn

Posts: 9

posted 9 years ago

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.

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.