18

I've been using String[] split(String) of String class to split any string for some given delimiter, and it worked fine.

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.

Also, I feel that String[] returned by split() in a single call is much efficient option than using the objects of the class StringTokenizer.

2
  • 2
    Refer this: stackoverflow.com/questions/691184/… Commented Oct 30, 2013 at 9:07
  • 1
    StringTokenizer is rarely used these days. I would have a look at Scanner. Commented Oct 30, 2013 at 9:08

5 Answers 5

22

Take a look at the JavaDocs

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.

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]); 
Sign up to request clarification or add additional context in comments.

Comments

2

String#split accepts a regular expression whether StringTokenizer just accepts a String by which will split the string. You should always stick to the String#split, it's more robust then StringTokenizer.

Comments

2

Read this

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.

Comments

2

I have the following program,

The string "x" is a tab separated 12s34;

 public class Testoken { public static void main(String[] args) { String x = "1 2 s 3 4 "; StringTokenizer st = new StringTokenizer(x,"\t"); int i = 0; while(st.hasMoreTokens()){ System.out.println("token-->"+st.nextToken()); i++; } System.out.println("i-->"+i);//elements from tokenizer String [] a = x.split("\t"); System.out.println("length--->"+a.length); for(int y = 0;y<a.length;y++){ System.out.println("value-->"+a[y]);//elements from split } } } Output: token-->1 token-->2 token-->s token-->3 token-->4 i-->5 length--->8 value-->1 value-->2 value-->s value--> value-->3 value--> value--> value-->4 

1 Comment

Output: token-->1 token-->2 token-->s token-->3 token-->4 i-->5 length--->8 value-->1 value-->2 value-->s value--> value-->3 value--> value--> value-->4
0

http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html say:

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.

So I would say, don't change it and show that line to the person who recommended refactoring it. Maybe they have old information or another good reason to tell you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.