This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Tokenizer

 
sreeni san
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I need to tokenize a string with ',' as delimiter.
for example in the string below:
Sreeni, san, 12345, , #44 XYZ street,,234567
Here I need to tokenize with comma as delimiter.
The problem is with the value is not specified and instead when ,, is specified. The StringTokenizer skips this as a value, whereas , , is returned as blank.
How do I overcome this problem, when ,, is specified.
Note: comma character in the above string is not a value.
Thanks
Sreeni
 
aminur rashid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sreeni.
I have tried to solve ur problem.You can have other logic for this also.What i have done in this function is that i have used a temproryTokenizer.From this temproryTokeniser i have eliminated the tokens with values" " and null
Then i have made the finalToknizer

cheers
 
sreeni san
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
Thanks for the reply.
The problem I have is that I need to retrieve token when the value is ,, . The StringTokenizer just skips this.
I'll try to modify my example:
I have data pertaining to a user for the fields below:
fName, lname, userId, address1, address2,city, phone
The corresponding values for the above fields are
Sreeni, san, 12345, , #44 XYZ street,,234567
Now I tokenize the entire String and assign values to respective variables. While doing so I have encountered problems like for example:
The value for fName, lname, userId, address1, address2 are returned correctly as tokens.
The problem here is with city as the value ",," is skipped and 234567 is returned as next token immediately after address2.
In case of address1 ", ," is value is returned as blank, which is fine for me.
Kindly suggest me a suitable solution.

thanks
Sreeni
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you replace ,, with , , or ,"", or ,null, before you use the StringTokenizer?
 
aminur rashid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello the problem with that is ''while ' ' makes a string with a space character.
You can try one thing .Use the constructer

It will return values the delimiter
, after every string.Write a method to create a null or "" string if u get two , without getting any values.Hope this will help
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using SDK 1.4, you can handle this easily using the String class' split() method. Otherwise, here's a class I wrote some time ago to solve this problem:

[ April 03, 2002: Message edited by: Jim Yingst ]
 
Steve Deadsea
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a stringtokenizer that can handle null tokens:
http://ostermiller.org/utils/StringTokenizer.html
I also wrote a CSV parser that will even handle quoted values:
http://ostermiller.org/utils/CSVLexer.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic