views:

30

answers:

3

I have a string that has comma separated values. How can I count how many elements in the string separated by comma. e.g following string has 4 elements

string = "1,2,3,4";

+4  A: 

myString.split(',').length

Pavel Radzivilovsky
A: 

First split it, and then count the items in the array. Like this:

"1,2,3,4".split(/,/).length;
WoLpH
+1  A: 
var mystring = "1,2,3,4";
var elements = mystring.split(',');
return elements.length;
erjiang