0

I have a string variable that has exactly 44 characters:

02081516171821242936374750565865666871737476

I need to split it into an array like this:

arr[0]=02
arr[1]=08
.
.
arr[21]=76

How can I do that? Thanks.

EDIT:

I know it must be easy but I couldn't find the necessary jquery functions to do it. Here is the pseudocode:

var s = "02081516171821242936374750565865666871737476";
var tmp;
var index=0;
for i =0 to 21
  arr[0] = mid(s,index,2); // take two characters starting from "index"
  index=index+2;
  next i

I just need the syntax.

WhoCares
  • 225
  • 1
  • 5
  • 16

4 Answers4

6

You could split using .match

"02081516171821242936374750565865666871737476".match(/\d{2}/g)

that returns

[
 "02", "08", "15", "16", "17", "18", "21", "24", "29", "36", "37", 
 "47", "50", "56", "58", "65", "66", "68", "71", "73", "74", "76"
]
  • 1
    Mentioning that it returns `["02", "08", "15", "16", "17", "18", "21", "24", "29", "36", "37", "47", "50", "56", "58", "65", "66", "68", "71", "73", "74", "76"]` will be extra ++ – techfoobar Jul 27 '15 at 16:52
0

var str = '02081516171821242936374750565865666871737476';
var arr = new Array();
var x = 0;
if(x < 22){
for(var i=0;i<str.length;i=i+2){
   arr[x] = str.substring(i, i+2);
  x++;
}
  }

console.log(arr);
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
Keerthi
  • 923
  • 6
  • 22
0
n=[]
a="02081516171821242936374750565865666871737476".split('')
while(a.length) n.push(a.splice(0,2).join(''))
xyz
  • 3,349
  • 1
  • 23
  • 29
0

You can call any symbol from string using: charAt() function

JS:

var data = '02081516171821242936374750565865666871737476';
var res = new Array();

for (var i = 0, count = 0; i < data.length; i += 2, count++ ) {

    res[count] = data.charAt(i) + data.charAt(i+1);

}
WinK-
  • 372
  • 2
  • 14