Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help Required in using StringTokenizer

 
Ramesh R G V S
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
jay zhuang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to split a string like "A.B.E" ?

String[] s = "A.B.E".split(".");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
does not work.
 
Vishwanath Sinha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks, Barry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic