admin管理员组

文章数量:1200344

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?

Share Improve this question asked Oct 25, 2010 at 18:34 chromedudechromedude 4,30216 gold badges69 silver badges96 bronze badges 3
  • Do you really mean "certain character numbers" as specified in the title , or at "every single character" as in the body of the question? – jball Commented Oct 25, 2010 at 18:40
  • I thought they would be related. Because I was thinking it would be every 1 character in this case, but in other cases it would be maybe every three characters. I was going for a broader question that I thought was related. – chromedude Commented Oct 25, 2010 at 18:44
  • 1 The solution to the "certain number" yields a solution to "every single", but the reverse is not necessarily true. For example @Harmen's answer below would be a strange starting point for splitting a string into 3 character chunks. – jball Commented Oct 25, 2010 at 18:51
Add a comment  | 

7 Answers 7

Reset to default 10

I was researching a similar problem.. to break on every other character. After reading up on regex, I came up with this:

data = "0102034455dola";
arr = data.match(/../g);

The result is the array: ["01","02","03","44","55","do","la"]

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;
}
var s= "hello";
s.split("");

Here is a simple way do it with a while loop;

function splitStringAtInterval(str, len){
var len = 10;
var arr = [];
str = str.split("");
while(str.length > len){
    arr.push(str.splice(position,len).join(""));
}
if(str.length > 0)arr.push(str.join(""));
    return arr;
}

If you want it short and 'functional':

var input = 'abcdefghijklmn1234567890';
var arr = Array.prototype.slice.call(input), output = [];
while (arr.length) output.push(arr.splice(0, 3).join(''));

output; // ["abc", "def", "ghi", "jkl", "mn1", "234", "567", "890"]

If you don't want to use a RegExp, you can also do this instead:

const splitEvery = (nth) => (str) =>
  Array.from(
    {length: Math.ceil(str.length / nth)},
    (_, index) => str.slice(index * nth, (index + 1) * nth)
  )

// example usage:
const splitEvery2nd = splitEvery(2)
const result = splitEvery2nd('hello')
// result is: ['he', 'll', 'o']

If you want to cut off any remaining parts, replace the Math.ceil with a Math.floor call.

Understanding:

This function creates an Array with the length of the number of slices, containing the expected parts of the text.

本文标签: How do you split a string at certain character numbers in javascriptStack Overflow