• 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

Help Required in using StringTokenizer

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am using StringTokenizer class to parse a string but its missing some empty tokens.
Ex = "ramesh,krishns,kishore,,venu,raja"
its giving the output as
ramesh
krishns
kishorevenu
raja
i wena to trap the ,, also
how to do
Regards
Ramesh R G V S
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are correct. Consecutive delimiters appear to be thrown away.
Perhaps using the StringTokenizer constructor which has three arguments, the last being a returnDelims boolean will help you solve your problem. Setting returnDelims to true will return the delimiter strings (commas in your case) as well as the token strings.
You can the detect the occurence of two consecutive tokens with some extra code.
-Barry
[ December 26, 2002: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Jim Yingst, from another post, I see that there is a method of String added to Java 1.4 which you can use.
String[] split(String regexp);
It works with a string like "A,B,,,E". However,
if you have "A,B,C,," you will have to look after the last trailing empty string yourself it seems.

-Barry
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to split a string like "A.B.E" ?

String[] s = "A.B.E".split(".");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
does not work.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramesh,
To get trap the delimeters also use the constructor option for StringTokenizer as

StringTokenizer(String str, String delim,
boolean true)
if we use this constructor for StringTokenizer then the delimeter character are also returned as tokens. And if the flag is false, then the delimeter chatarcter are skipped and delimeter is only used as delimeters between the tokens.
Default flag is false.
Regards,
Vishwanath

Originally posted by Ramesh R G V S:
Hi
I am using StringTokenizer class to parse a string but its missing some empty tokens.
Ex = "ramesh,krishns,kishore,,venu,raja"
its giving the output as
ramesh
krishns
kishorevenu
raja
i wena to trap the ,, also
how to do
Regards
Ramesh R G V S

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jay, I wouldn't expect it to. The argument to split is a String representing a regular expression.
If instead of using "." as an argument, you use "\\." you will find that it works. You must use two backslashes.
Read the API and learn about regular expressions.
-Barry
[ December 30, 2002: Message edited by: Barry Gaunt ]
 
jay zhuang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Barry.
 
Good night. Drive safely. Here's a tiny ad for the road:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic