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.

Satyendra
  • 1,547
  • 2
  • 16
  • 30

5 Answers5

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]);
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
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.

Petr Mensik
  • 24,455
  • 13
  • 84
  • 111
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.

Ankit Rustagi
  • 5,203
  • 10
  • 34
  • 65
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
user2114253
  • 169
  • 1
  • 4
  • 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 – user2114253 May 18 '14 at 08:09
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.

koljaTM
  • 9,518
  • 2
  • 36
  • 41