aspose file tools
The moose likes Java in General and the fly likes Difference between split() and StringTokenizer s nextToken() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Difference between split() and StringTokenizer s nextToken()" Watch "Difference between split() and StringTokenizer s nextToken()" New topic
Author

Difference between split() and StringTokenizer s nextToken()

Deepak Kumar
Ranch Hand

Joined: Nov 05, 2007
Posts: 59
Hi All,

I have the following program,

The string "x" is a tab separated
1<tab>2<tab>s<tab><tab>3<tab><tab><tab>4<tab>;





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



Why there is a discrepency in the output from tokenizer and split?

Thanks in advance.

Regards,
Deepak


Thanks,
Deepak
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

String.split() does it the right way. Between the s and 3, there are two tabs, and between the 3 and 4 there are three tabs. Between those tabs, there are actually empty strings. Those show up, whereas StringTokenizer simply ignores them.

Now, if you want to remove those empty strings, change the regular expression of String.split to "\t+". The + means once or more.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Deepak Kumar
Ranch Hand

Joined: Nov 05, 2007
Posts: 59
Hi Rob,

Since split is returning 8 elements and tokenizer is returning 5 element.
So How can I return 8 elements with use of StringTokenizer only?

Regards,
Deepak
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

One way would be to use the constructor with a boolean, and make the StringTokenizer return the delimiters as well. But then you'd have to handle the delimiters as special cases. Not a nice solution at all.

The other option (and my favourite): don't use StringTokenizer at all. String.split does just what you want, and you can mimick StringTokenizer's behaviour very easily with String.split; the other way around is harder as you have just noticed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between split() and StringTokenizer s nextToken()
 
Similar Threads
Int array to 3D array
Split using tab delimiter
MultiDimenaional Array sort
Stack Problem
Manipulating Vectors (code not working)