| Author |
StringTokenizer and the "" empty string
|
jite eghagha
Ranch Hand
Joined: Oct 06, 2006
Posts: 124
|
|
Hello, My strings look like this: "001668","RTOFENTRY","LICENSE","","SJC","","00000431","Rental Car" Notice the "" empty string. My StringTokenizer's constructor takes care of delimiter characters StringTokenizer token = new StringTokenizer(str, ",\""); The problem is that the "" empty string is not counted as a token! So instead of my token count to be 8, i get a 6. How would you deal with this. Any suggestions besides me using a StringTokenizer. Thanks for your time
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
|
What do you want the output to look like? What I'm asking is do you want the quotes included in the output, do you want just the characters between the quotes? Do you want to retain the commas?
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
jite eghagha
Ranch Hand
Joined: Oct 06, 2006
Posts: 124
|
|
"001668","RTOFENTRY","LICENSE","","SJC","","00000431","Rental Car" I'd like my output to look like this: 001668 RTOFENTRY LICENSE "" SJC "" 00000431 Rental Car This would be a row in a JTable, so the "" would actually just be an empty string or perhaps a null. No commas or quotes in the out put. [ August 08, 2008: Message edited by: jite eghagha ]
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
This is probably the most straightforward way to do it. It assumes that the input Strings will always be surrounded by "". If that's not the case, yuo may want to look into a more robust solution using the java.util.Pattern and java.util.Matcher regex classes.
|
 |
Sachin Joshi
Ranch Hand
Joined: Aug 06, 2008
Posts: 83
|
|
This is what I tried...Same as your code just have only token .... I m getting tokencount as 8 ....also note that relaceAll for qoute(").... Hope this helps....
|
Web Development Tips and Tutorials - By Sachin
|
 |
jite eghagha
Ranch Hand
Joined: Oct 06, 2006
Posts: 124
|
|
|
thanks a lot, both solutions worked out.
|
 |
Rene Larsen
Ranch Hand
Joined: Oct 12, 2001
Posts: 1179
|
|
You can also use the org.apache.commons.lang.text.StrTokenizer class from the Apache Commons Lang Project. This class has some helper metheds, e.g. the method 'setIgnoreEmptyTokens(boolean)'
|
Regards, Rene Larsen
Dropbox Invite
|
 |
 |
|
|
subject: StringTokenizer and the "" empty string
|
|
|