Reputation: 2876
I have a string in the following format :
One,Two,Three,Four
and I want to change its format to "One","Two","Three","Four"
I tried the following :
var items = ['One,Two,Three,Four'];
var quotedAndCommaSeparated = '"' + items + '"';
document.write(quotedAndCommaSeparated);
which adds the double quotes at the beginning and at the end of the string. I don't want to use replace
because there might be values that have a comma.
Is there a way to split the initial string and return the wanted one?
Upvotes: 0
Views: 135
Reputation: 4302
This should give you what you want. Split on the commas and then rejoin using the delimiter you are looking for.
var quotedAndCommaSeparated = '"'+items[0].split(',').join('","')+'"'
Upvotes: 1