[javascript] How to split string and push in array using jquery

var string = 'a,b,c,d';
var array = [];

As we can see above I have one string values with separator*(,)*. I want to split these values and wants to push in the array. At last I want to read my array by using for loop. Please suggest.

This question is related to javascript jquery

The answer is


Use the String.split()

var array = string.split(',');

var string = string.split(",");

var string = 'a,b,c,d',
    strx   = string.split(',');
    array  = [];

array = array.concat(strx);
// ["a","b","c","d"]

You don't need jQuery for that, you can do it with normal javascript:

http://www.w3schools.com/jsref/jsref_split.asp

var str = "a,b,c,d";
var res = str.split(","); // this returns an array