How to split strings at specific intervals to arrays in javascript -
how split strings @ specific interveals arrays in javascript
for example: split string 4 characters (including space , characters)
this example should split,numbers(123),space,characters included
to
this ------> 1st array ------> 2nd array ------> 3rd array exam ------> 4th array ple ------> 5th array shou ------> 6th array ............ etc till..... ..ed ------> last array
try this:
var foo = "this example should split,numbers(123),space,characters included"; var arr = []; (var = 0; < foo.length; i++) { if (i % 4 == 0 && != 0) arr.push(foo.substring(i - 4, i)); if (i == foo.length - 1) arr.push(foo.substring(i - (i % 4), i+1)); } document.write(arr); console.log(arr);
Comments
Post a Comment