Scanner vs. StringTokenizer vs. String.Split

来源:互联网 发布:计算天数的软件 编辑:程序博客网 时间:2024/06/15 20:25

从stack overflow上面看到java输入的对比答案,附链接:

http://stackoverflow.com/questions/691184/scanner-vs-stringtokenizer-vs-string-split


They're essentially horses for courses.

  • Scanner is designed for cases where you need to parse a string, pulling out data of different types. It's very flexible, but arguably doesn't give you the simplest API for simply getting an array of strings delimited by a particular expression.
  • 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 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 whichString.split() is a part.

You'll note from my timings that String.split() can still tokenizethousands of strings in a few milliseconds on a typical machine. In addition, it has the advantage overStringTokenizer that it gives you the output as a string array, which is usually what you want. Using anEnumeration, 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 useString.split().


0 0
原创粉丝点击