我想把一个非常大的字符串(比如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 ' ]
其他回答
我已经写了一个扩展函数,所以块长度也可以是一个数字数组,比如[1,3]
String.prototype.chunkString = function(len) {
var _ret;
if (this.length < 1) {
return [];
}
if (typeof len === 'number' && len > 0) {
var _size = Math.ceil(this.length / len), _offset = 0;
_ret = new Array(_size);
for (var _i = 0; _i < _size; _i++) {
_ret[_i] = this.substring(_offset, _offset = _offset + len);
}
}
else if (typeof len === 'object' && len.length) {
var n = 0, l = this.length, chunk, that = this;
_ret = [];
do {
len.forEach(function(o) {
chunk = that.substring(n, n + o);
if (chunk !== '') {
_ret.push(chunk);
n += chunk.length;
}
});
if (n === 0) {
return undefined; // prevent an endless loop when len = [0]
}
} while (n < l);
}
return _ret;
};
的代码
"1234567890123".chunkString([1,3])
将返回:
[ '1', '234', '5', '678', '9', '012', '3' ]
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
function chunkString(str, length = 10) {
let result = [],
offset = 0;
if (str.length <= length) return result.push(str) && result;
while (offset < str.length) {
result.push(str.substr(offset, length));
offset += length;
}
return result;
}
我对上述解决方案的问题是,不管在句子中的位置如何,它都将字符串划分为正式的大小块。
我认为下面的方法比较好;虽然它需要一些性能调整:
static chunkString(str, length, size,delimiter='\n' ) {
const result = [];
for (let i = 0; i < str.length; i++) {
const lastIndex = _.lastIndexOf(str, delimiter,size + i);
result.push(str.substr(i, lastIndex - i));
i = lastIndex;
}
return result;
}
这是一个快速而直接的解决方案
function chunkString (str, len) { const size = Math.ceil(str.length/len) const r = Array(size) let offset = 0 for (let i = 0; i < size; i++) { r[i] = str.substr(offset, len) offset += len } return r } console.log(chunkString("helloworld", 3)) // => [ "hel", "low", "orl", "d" ] // 10,000 char string const bigString = "helloworld".repeat(1000) console.time("perf") const result = chunkString(bigString, 3) console.timeEnd("perf") console.log(result) // => perf: 0.385 ms // => [ "hel", "low", "orl", "dhe", "llo", "wor", ... ]