使用 StringTokenizer 和 String.split() 的区别? [英] Difference between using StringTokenizer and String.split( )?
问题描述
我一直在使用 String
类的 String[] split(String)
来拆分任何给定定界符的字符串,并且效果很好.
I've been using String[] split(String)
of String
class to split any string for some given delimiter, and it worked fine.
然而,现在预计使用 StringTokenizer
重构相同的逻辑.但是,使用一种与另一种相比有什么区别和好处.
However, now it is expected to re-factor the same logic with StringTokenizer
. But what are the differences and benifits of using one over the other.
另外,我觉得 split()
在一次调用中返回的 String[]
比使用 StringTokenizer<类的对象更有效/代码>.
Also, I feel that String[]
returned by split()
in a single call is much efficient option than using the objects of the class StringTokenizer
.
推荐答案
查看JavaDocs
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 方法用于将字符串分解为其基本标记:
The following example illustrates 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");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);
这篇关于使用 StringTokenizer 和 String.split() 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!