4

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?

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? 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. 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. Commented Oct 25, 2010 at 18:51

7 Answers 7

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"]

Sign up to request clarification or add additional context in comments.

Comments

8

Yes, you could use:

var str = "hello";

// returns ["h", "e", "l", "l", "o"]
var arr = str.split( '' ); 

1 Comment

ok, thanks (I will mark you as the answer once I am allowed [after 15mins])
5

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;
}

1 Comment

thanks, even though that wasn't my actual question I was looking for this answer for future reference.
1
var s= "hello";
s.split("");

Comments

0

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;
}

Comments

0

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"]

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.