我想把一个非常大的字符串(比如10,000个字符)分割成n大小的块。
就性能而言,最好的方法是什么?
例如: "1234567890"除以2将变成["12","34","56","78","90"]。
使用string。prototype。match可以实现这样的事情吗如果可以,从性能来看,这是最好的方式吗?
我想把一个非常大的字符串(比如10,000个字符)分割成n大小的块。
就性能而言,最好的方法是什么?
例如: "1234567890"除以2将变成["12","34","56","78","90"]。
使用string。prototype。match可以实现这样的事情吗如果可以,从性能来看,这是最好的方式吗?
当前回答
你绝对可以做一些
let pieces = "1234567890 ".split(/(.{2})/).filter(x => x.length == 2);
要得到这个:
[ '12', '34', '56', '78', '90' ]
如果你想动态输入/调整数据块的大小,使数据块的大小为n,你可以这样做:
n = 2;
let pieces = "1234567890 ".split(new RegExp("(.{"+n.toString()+"})")).filter(x => x.length == n);
要在原始字符串中找到所有大小为n的块,尝试这样做:
let subs = new Set();
let n = 2;
let str = "1234567890 ";
let regex = new RegExp("(.{"+n.toString()+"})"); //set up regex expression dynamically encoded with n
for (let i = 0; i < n; i++){ //starting from all possible offsets from position 0 in the string
let pieces = str.split(regex).filter(x => x.length == n); //divide the string into chunks of size n...
for (let p of pieces) //...and add the chunks to the set
subs.add(p);
str = str.substr(1); //shift the string reading frame
}
你应该得到:
[ '12', '23', '34', '45', '56', '67', '78', '89', '90', '0 ' ]
其他回答
你可以这样做:
"1234567890".match(/.{1,2}/g);
// Results in:
["12", "34", "56", "78", "90"]
如果字符串的大小不是chunk-size的倍数,该方法仍然有效:
"123456789".match(/.{1,2}/g);
// Results in:
["12", "34", "56", "78", "9"]
一般来说,对于任何你想要提取最多n个子字符串的字符串,你可以这样做:
str.match(/.{1,n}/g); // Replace n with the size of the substring
如果你的字符串可以包含换行符或回车,你会这样做:
str.match(/(.|[\r\n]){1,n}/g); // Replace n with the size of the substring
至于性能,我用了大约10k个字符,在Chrome上花了一秒钟多一点的时间。YMMV。
这也可以用在可重用函数中:
function chunkString(str, length) {
return str.match(new RegExp('.{1,' + length + '}', 'g'));
}
window.format = function(b, a) {
if (!b || isNaN(+a)) return a;
var a = b.charAt(0) == "-" ? -a : +a,
j = a < 0 ? a = -a : 0,
e = b.match(/[^\d\-\+#]/g),
h = e && e[e.length - 1] || ".",
e = e && e[1] && e[0] || ",",
b = b.split(h),
a = a.toFixed(b[1] && b[1].length),
a = +a + "",
d = b[1] && b[1].lastIndexOf("0"),
c = a.split(".");
if (!c[1] || c[1] && c[1].length <= d) a = (+a).toFixed(d + 1);
d = b[0].split(e);
b[0] = d.join("");
var f = b[0] && b[0].indexOf("0");
if (f > -1)
for (; c[0].length < b[0].length - f;) c[0] = "0" + c[0];
else +c[0] == 0 && (c[0] = "");
a = a.split(".");
a[0] = c[0];
if (c = d[1] && d[d.length -
1].length) {
for (var d = a[0], f = "", k = d.length % c, g = 0, i = d.length; g < i; g++) f += d.charAt(g), !((g - k + 1) % c) && g < i - c && (f += e);
a[0] = f
}
a[1] = b[1] && a[1] ? h + a[1] : "";
return (j ? "-" : "") + a[0] + a[1]
};
var str="1234567890";
var formatstr=format( "##,###.", str);
alert(formatstr);
This will split the string in reverse order with comma separated after 3 char's. If you want you can change the position.
你可以在没有正则表达式的情况下使用reduce():
(str, n) => {
return str.split('').reduce(
(acc, rec, index) => {
return ((index % n) || !(index)) ? acc.concat(rec) : acc.concat(',', rec)
},
''
).split(',')
}
var str = "123456789";
var chunks = [];
var chunkSize = 2;
while (str) {
if (str.length < chunkSize) {
chunks.push(str);
break;
}
else {
chunks.push(str.substr(0, chunkSize));
str = str.substr(chunkSize);
}
}
alert(chunks); // chunks == 12,34,56,78,9
使用npm库"chkchars" 但是请记住,要确保给定的字符串长度完全除以“number”参数。
const phrase = "1110010111010011100101110100010000011100101110100111001011101001011101001110010111010001000001110010111010011100101110100"
const number = 7
chkchars.splitToChunks(phrase, number)
// result => ['1110010', '1110100','1110010', '1110100','0100000', '1110010','1110100', '1110010','1110100', '1011101','0011100', '1011101','0001000','0011100','1011101', '0011100','1011101']
// perf => 0.287ms