tags:

views:

35

answers:

3

I want a string split at every single character and put into an array. The string is:

var string = "hello";

Would you use the .split() ? If so how?

+4  A: 

Yes, you could use:

var str = "hello";

// returns ["h", "e", "l", "l", "o"]
var arr = str.split( '' ); 
Harmen
ok, thanks (I will mark you as the answer once I am allowed [after 15mins])
chromedude
+1  A: 
var s= "hello";
s.split("");
Dustin Laine
+2  A: 

If you really want to do it as described in the title, this should work:

function splitStringAtInterval (string, interval) {
var result = [];
for (var i=0; i<string.length; i+=interval)
  result.push(string.substring (i, i+interval));
return result;
}
rob
thanks, even though that wasn't my actual question I was looking for this answer for future reference.
chromedude