The Java documentation doesn't seem to mention anything about deprecation for StringTokenizer
, yet I keep hearing about how it was deprecated long ago. Was it deprecated because it had bugs/errors, or is String.split()
simply better to use overall?
I have some code that uses StringTokenizer
and I am wondering if I should seriously be concerned about refactoring it to use String.split()
, or whether the deprecation is purely a matter of convenience and my code is safe.
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
The following example shows how the String.split() method can be used to break up a string into its basic tokens:
There is an issue with StringTokenize ...
Split have to use regex, StringTokenizer is using String or CharSequence,
but
"a.b..".split(".")
will return{"a","b",""}
and StringTokenizer of "a.b.." ... will return only {"a","b"}
And this is very tricky!!! Be Carefull!!!
Better and safer alternatives to StringTokenizer are:
Much better
StrongTokenizer
is inorg.apache.common.lang3
... it have much more flexibility orcom.google.common.base.Splitter