• Post Reply Bookmark Topic Watch Topic
  • New Topic

A little bit more than the StringTokenizer  RSS feed

 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a comma-delimited list of values that I'd like to be able to have tokenized and automatically placed into a list or an array. Is there such an object that will allow me to do that via the constructor or something?
The manual alternative would seem to be to use StringTokenizer, iterate over it's values, and insert them into a list. I'd like to have to avoid this and use the desired above solution if possible.
Thanks for the input.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using JDK 1.4 or later, you can use String's split() method. E.g.

You may find it preferable to use

instead, if you want to make sure you get array elements for all trailing empty elements as well. Also beware that your file format may allow some sort of escape sequence for fields that contain commas themselves or other weird things, which can complicate things. The (first) argument to split() is a regular expression, so you can actually put more complex things there, depending onthe situation. (You can also confuse yourself quite a bit if you're not familiar with regexes - it's worthwhile to take some time to learn.) Jeffrey Friedl's Mastering Regular Expressions has an extended discussion of some of the subtleties of parsing CSV files. And it's a great book in general too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!