我有一个字符串变量,正好有44个字符:
02081516171821242936374750565865666871737476
我需要将它拆分成这样的数组:
arr[0]=02
arr[1]=08
.
.
arr[21]=76
我该怎么做?感谢。
修改
我知道它一定很容易但我找不到必要的jquery函数来完成它。这是伪代码:
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
我只需要语法。
答案 0 :(得分:6)
您可以使用.match
"02081516171821242936374750565865666871737476".match(/\d{2}/g)
返回
[
"02", "08", "15", "16", "17", "18", "21", "24", "29", "36", "37",
"47", "50", "56", "58", "65", "66", "68", "71", "73", "74", "76"
]
答案 1 :(得分: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);
&#13;
答案 2 :(得分:0)
marginBottom
答案 3 :(得分:0)
您可以使用:charAt()
函数
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);
}