June 15, 2012

Is there any functional difference between StringTokenizer and split() function of String?


Yes,

StringTokenizer, the delimiter is just one character long. You supply a list of characters that count as delimiters, but in that list, each character is a single delimiter. With split(), the delimiter is a regular expression, which is something much more powerful (and more complicated to understand). It can be any length. Regular expressions may be harder to understand at first, but when you learn how to use them, they're much more useful.


Also, if you need to parse empty tokens, e.g. a comma-separated line like


one,,three,,,six


where the field values are "one", "", "three", "", "" and "six" where the three empty strings are indicated by the commas with nothing between them - that's a lot more work with a StringTokenizer. By default it gives you just "one", "three", "six" and skips the empties. You can use a special constructor that takes a boolean to tell the StringTokenizer to return delimiters, but that gets complicated too. I'll skip the details. It's much easier to use split(","), which immediately returns {"one", "", "three", "", "", "six"), exactly right. The short version is: StringTokenizer doesn't handle empty strings well. But split() does.
Suppose , if you take small example


The string "x" is a tab separated
1<tab>2<tab>s<tab><tab>3<tab><tab><tab>4<tab>;
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:

  1. I have also started creating blog on java. Its in still new phase.
    You are invited to see my blog and put your valuable comments
    http://javacodeimpl.blogspot.com

    ReplyDelete

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts