为什么不推荐使用 StringTokenizer? [英] Why is StringTokenizer deprecated?

查看:36
本文介绍了为什么不推荐使用 StringTokenizer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 文档似乎没有提到任何关于 StringTokenizer 的弃用,但我一直听说很久以前它是如何被弃用的.它是否因为存在错误/错误而被弃用,还是 String.split() 更适合整体使用?

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?

我有一些使用 StringTokenizer 的代码,我想知道我是否应该认真考虑重构它以使用 String.split(),或者是否弃用纯粹是为了方便,我的代码是安全的.

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:

StringTokenizer 是一个遗留类,出于兼容性原因保留,但不鼓励在新代码中使用它.建议任何寻求此功能的人改用 String 的 split 方法或 java.util.regex 包.

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.

如果你看看 String.split()StringTokenizer 比较,相关的区别在于 String.split() 使用正则表达式,而 StringTokenizer 只使用逐字拆分字符.所以如果我想用比单个字符更复杂的逻辑来标记一个字符串(例如在 上拆分),我不能使用 StringTokenizer 但我可以使用 <代码>String.split().

If you look at String.split() and compare it to StringTokenizer, the relevant difference is that String.split() uses a regular expression, whereas StringTokenizer just uses verbatim split characters. So if I wanted to tokenize a string with more complex logic than single characters (e.g. split on ), I can't use StringTokenizer but I can use String.split().

这篇关于为什么不推荐使用 StringTokenizer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆