I want to split a string in Java script using
str.split([separator[, limit]])
I want to split it by a space, but ' '
did not work.
What should I use?
I want to split a string in Java script using
str.split([separator[, limit]])
I want to split it by a space, but ' '
did not work.
What should I use?
This question has previously been asked here: How do I split a string, breaking at a particular character?
In your specific case, based on what you provided, it appears you are attempting to split on nothing. ''
instead of an actual space ' '
.
To replace the spaces by commas:
var str = "How are you doing today?";
var res = str.split(" ");
//Output: How,are,you,doing,today?
Like described here: http://www.w3schools.com/jsref/jsref_split.asp
Another option is to use str.replace:
Var str = "Mr Blue has a blue house and a blue car";
var res = str.replace(/blue/gi, "red");
//Output: Mr red has a red house and a red car
Like described here: http://www.w3schools.com/jsref/jsref_replace.asp
But what you may actually wanted:
var str = "Test[separator[, limit]]Test";
var res = str.split("[separator[, limit]]").join(" ");
//Output: Test Test