• Post Reply Bookmark Topic Watch Topic
  • New Topic

String splitting & tokenizing problem  RSS feed

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a string as

String off="offer,1,,,,3,";

With both StringTokenizer(",") and off.split(",")

the last value is not getting retrieved....

how to overcome this.....

According to Sun's split functions documentation
Split:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

regards
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you have two options:
- Set the StringTokenizer to include the separators and check whether the last token is a separator.
- If you expect a fixed number of tokens, just count the processed ones and add a blank at the end when you miss one.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the no of tokens are not fixed.........

and

if the string is

String off="offer,,,,,,";

i get only offer
[ October 24, 2007: Message edited by: A Kumar ]
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also the

constructor, where the 'true' means you also want the delimiters.
In that case, you get all the "," as separate tokens. You don't get the blanks anymore, you have to calculate them based on the fact you get two delimiters after each other.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the documentation for split(String, int). If the second argument is negative, then trailing empty strings will not be discarded - that's what you want here.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Check the documentation for split(String, int). If the second argument is negative, then trailing empty strings will not be discarded - that's what you want here.


Thanks, I didn't know that one either. Apparently it's time to forget about StringTokenizer and use this split more often.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!