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?
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?
Yes, you could use:
var str = "hello";
// returns ["h", "e", "l", "l", "o"]
var arr = str.split( '' );
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;
}