Tuesday, March 1, 2016

随风而逝的StringTokenizer[StackOverflow问答] | 书影博客



随风而逝的StringTokenizer[StackOverflow问答] | 书影博客

数年以前,在刚开始接触Java时,曾经学习过一个用于分割字符串的工具类:StringTokenizer。

例如将字符串"this is a test"按照空白字符进行分割,StringTokenzier的用法示例如下:

StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) {     System.out.println(st.nextToken()); } 
  • 1
  • 2
  • 3
  • 4

虽然Java文档中似乎没有将StringTokenizer标记为deprecated,但实际上这个类已经很少有人在使用了。其功能已经为String.split()方法所取代,上例的字符串可以使用String.split()方法进行分割:

String tokens[] = "this is a test".split(); for (String token : tokens) {     System.out.println(token); } 
  • 1
  • 2
  • 3
  • 4

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.

上面的文字引用自JavaDoc:"StringTokenizer是一个遗留类,之所以被保留是出于兼容性的考虑,但是在编写新的代码时已经不再鼓励使用。如果需要使用其功能,建议使用String类的split方法或者java.util.regex包取而代之"。

Deprecation only happens when the class/method has some serious drawbacks. A similar situation happens with Vector: you can almost always replace it with an ArrayList, but it's not "bad" or "broken", therefore it's not deprecated.

只有当某个类或者方法包含严重的缺陷时,才会被标记为"过时(deprecated)"。与之类似的情形还有Vector:你几乎总是可以使用ArrayList替代Vector,但这并不是说Vector不好或者不能用,因此Vector并没有被标记为deprecated

如果观察一下String.split()并将其与StringTokenizer进行比较,可以发现它们的不同之处:String.split()使用的是正则表达式,而StringTokenizer只是简单地逐字拆分。因此如果需要使用比单个字符更加复杂的逻辑对字符串进行拆分时,就只能放弃StringTokenizer,使用String.split()。

参考:http://stackoverflow.com/questions/6983856/why-is-stringtokeniz


Read full article from 随风而逝的StringTokenizer[StackOverflow问答] | 书影博客

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts