I am trying to find a way to split a string for every character on JavaScript, an equivalent to String.ToCharArray() from c#
To later join them with commas.
ex: "012345" after splitting -> "['0','1','2','3','4','5']" after join -> "0,1,2,3,4,5"
So far what I have come across is to loop on every character and manually add the commas (I think this is very slow)
This is a much simpler way to do it:
The same thing, except with comments:
Notice that I pass an empty string to
split(). If you don’t pass anything, you’ll get an array containing only the original string, rather than an array containing each character.Alternatively you could pass nothing to
join()and it’d use a comma by default, but in cases like this I prefer to be specific.Don’t worry about speed — I’m sure there isn’t any appreciable difference. If you’re so concerned, there isn’t anything wrong with a loop either, though it might be more verbose.