• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

String Tokenizer

Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
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
  • 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?
[This message has been edited by Art Metzer (edited April 06, 2001).]
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • 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;
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;

The moustache of a titan! The ad of a flea:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic