Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you split a string at certain character numbers in javascript?

Tags:

javascript

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?

like image 698
chromedude Avatar asked Nov 28 '22 11:11

chromedude


2 Answers

Yes, you could use:

var str = "hello";

// returns ["h", "e", "l", "l", "o"]
var arr = str.split( '' ); 
like image 36
Harmen Avatar answered Nov 30 '22 23:11

Harmen


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

like image 108
Gary Avatar answered Dec 01 '22 01:12

Gary