为什么不赞成使用StringTokenizer? [英] Why is StringTokenizer deprecated?

查看:173
本文介绍了为什么不赞成使用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.

推荐答案

从javadoc for StringTokenizer

From the javadoc for 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 只使用逐字拆分字符。因此,如果我想用一个比单个字符更复杂的逻辑来标记一个字符串(例如,在 \\\\ n 上拆分),我就不能使用 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 \r\n), I can't use StringTokenizer but I can use String.split().

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

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