使用StringTokenizer和String.split()之间的区别? [英] Difference between using StringTokenizer and String.split( )?

查看:165
本文介绍了使用StringTokenizer和String.split()之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 String [] split(String) of String class来分割任何字符串给定分隔符,并且它工作正常。

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.

另外,我觉得 String [] 单个调用中返回的 split()比使用类 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.

推荐答案

- > String.split() Pattern.split()为后者提供了一个简单的语法,但这基本上就是他们所做的一切。如果你想解析生成的字符串,或者根据特定的标记改变分隔符的中途,它们将无法帮助你。

-> String.split() and Pattern.split() give you an easy syntax for doing the latter, but that's essentially all that they do. If you want to parse the resulting strings, or change the delimiter halfway through depending on a particular token, they won't help you with that.

- > StringTokenizer String.split()更具限制性,并且使用起来也有些小问题。它主要用于拉出由固定子串分隔的标记。由于这个限制,它的速度大约是 String.split()的两倍。 (请参阅我的比较 String.split() StringTokenizer 。)它也早于正则表达式API,其中 String.split()是一部分。

-> StringTokenizer is even more restrictive than String.split(), and also a bit fiddlier to use. It is essentially designed for pulling out tokens delimited by fixed substrings. Because of this restriction, it's about twice as fast as String.split(). (See my comparison of String.split() and StringTokenizer.) It also predates the regular expressions API, of which String.split() is a part.

你会从我的时间中注意到 String.split()仍然可以标记<在典型的机器上强大>几毫秒的数千个字符串。此外,它优于 StringTokenizer 它可以将输出作为字符串数组提供,这通常是您想要的。使用枚举,由 StringTokenizer 提供,在大多数情况下过于语法上挑剔。从这个角度来看, StringTokenizer 现在有点浪费空间,你也可以只使用 String.split()

You'll note from my timings that String.split() can still tokenize thousands of strings in a few milliseconds on a typical machine. In addition, it has the advantage over StringTokenizer that it gives you the output as a string array, which is usually what you want. Using an Enumeration, as provided by StringTokenizer, is too "syntactically fussy" most of the time. From this point of view, StringTokenizer is a bit of a waste of space nowadays, and you may as well just use String.split().

从此链接回答

这篇关于使用StringTokenizer和String.split()之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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