Why is StringTokenizer deprecated?

2018-12-31 22:26发布

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.

8条回答
回忆,回不去的记忆
2楼-- · 2018-12-31 23:31

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:

 String[] result = "this is a test".split("\\s");
查看更多
看风景的人
3楼-- · 2018-12-31 23:32

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 in org.apache.common.lang3 ... it have much more flexibility or

com.google.common.base.Splitter

查看更多
登录 后发表回答