| Author |
Help Required in using StringTokenizer
|
Ramesh R G V S
Greenhorn
Joined: Apr 20, 2002
Posts: 18
|
|
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
Joined: Aug 03, 2002
Posts: 7729
|
|
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 ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
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
Joined: Dec 15, 2002
Posts: 2
|
|
How to split a string like "A.B.E" ? String[] s = "A.B.E".split("."); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ does not work.
|
Jay
|
 |
Vishwanath Sinha
Greenhorn
Joined: Dec 23, 2002
Posts: 5
|
|
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
|
Vishwanath<br /> <br />IBM Certified Solution Developer - WebSphere Portal V6.0<br />IBM Certified SOA Associate<br />SCJP 1.4
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
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
Joined: Dec 15, 2002
Posts: 2
|
|
Thanks, Barry.
|
 |
 |
|
|
subject: Help Required in using StringTokenizer
|
|
|