Hi,
I'm curious, how would you test a string and say "yep, that is a comma delimited list!" I'm not so worried about the 'comma delimited' part more that this string has more than one item in it?
Thanks, R.
Hi,
I'm curious, how would you test a string and say "yep, that is a comma delimited list!" I'm not so worried about the 'comma delimited' part more that this string has more than one item in it?
Thanks, R.
You could also just go like:
var words = yourString.split(',');
for(var i=0;i<words.length;++i) {
doSomething(words[i]);
}
To check if the string has more than one item, try something like:
str.split(",").length > 1
... although, as suggested in a comment, correct parsing is likely to be bit more complicated than this for the general case.
whoops, misread language as Java - sorry.
Ask the string very politely if it's a comma delimited list. Perhaps even take it out for tea or an alcoholic beverage if it's open to that sort of thing. Just don't be too nice as you may end up in a long term relationship with it and end up marrying it. At which point it will slowly drain the life out of you and spend all of your money... but at least you can have intercourse with it.
Be very careful if you are just splitting on a comma for a csv list as fields can actually contain commas and are encased by quotes i.e.
Name,Age
"doe, jane",18
"bob, jim",20
If it isn't for a csv perhaps you should be using an array or an object to hold the values?