• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Tokenizer

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
During the use of the StringTokenizer class, I came across a string in the format "1","ABC","Last, First","CAT","MBA"
If i use the comma as a my delimiter, how do I handle the "," in inside the double quoted string? Especially when I do a countTokens(), it returns 6 tokens instead of 5.
Any help would be greatly appreciated.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Iphung",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I Since everything is a string in your example..
use "," as the 'token'.. BUT, you will have to use indexOf to get a position of each token and then subString() the token out of the String. This is because the StringTokenizer will consider the "," as a list of tokens to check for.
Ex:
"Hi","there","me,you"
Would give you 3 tokens.
token 1> "Hi
token 2> there
token 3> me, you"
Now all you have to do is chop the 1st char off the 1st token and the last char off the last token.
Just my $0.02cdn
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's all I could think of, and I of course defer to any better solutions.
1. Go through your original String, replacing all commas between quotes with a placeholder character that you can guarantee will never ever appear in any input String.
2. Tokenize this altered String.
3. When you receive each token, replace each placeholder you find with a comma.
4. Process the token normally.
Here's a bit of code to demonstrate:

Will something like this do the trick?
Art
[This message has been edited by Art Metzer (edited April 06, 2001).]
 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can use indexOf. Then get the length of string.
Here is an example:
int iLen;
iEnd = sToken.indexOf(':', iEnd);
sTemp = sToken.substring(iBegin, iEnd);
iBegin = iEnd + 1;
iEnd++;
if( iFlag == 1 ) // Parsing to get the values of arguments
{
iLen = Integer.parseInt(sTemp);
sTemp = sToken.substring(iBegin, iBegin + Len);
iEnd = iBegin + iLen + 1;
iBegin = iEnd;
}
return sTemp;

Thanks,
Angela
 
reply
    Bookmark Topic Watch Topic
  • New Topic