我想把一个非常大的字符串(比如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可以实现这样的事情吗如果可以,从性能来看,这是最好的方式吗?
当前回答
你可以在没有正则表达式的情况下使用reduce():
(str, n) => {
return str.split('').reduce(
(acc, rec, index) => {
return ((index % n) || !(index)) ? acc.concat(rec) : acc.concat(',', rec)
},
''
).split(',')
}
其他回答
惊喜!你可以使用split来分割。
var parts = "1234567890 ".split(/(.{2})/).filter(O=>O)
结果显示['12','34','56','78','90',' ']
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;
}
我已经写了一个扩展函数,所以块长度也可以是一个数字数组,比如[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' ]
它将大字符串拆分为给定单词的小字符串。
function chunkSubstr(str, words) {
var parts = str.split(" ") , values = [] , i = 0 , tmpVar = "";
$.each(parts, function(index, value) {
if(tmpVar.length < words){
tmpVar += " " + value;
}else{
values[i] = tmpVar.replace(/\s+/g, " ");
i++;
tmpVar = value;
}
});
if(values.length < 1 && parts.length > 0){
values[0] = tmpVar;
}
return values;
}
包括左版本和右版本的预分配。 对于小块,这和RegExp impl一样快,但是随着块大小的增加,速度会更快。它的内存效率很高。
function chunkLeft (str, size = 3) {
if (typeof str === 'string') {
const length = str.length
const chunks = Array(Math.ceil(length / size))
for (let i = 0, index = 0; index < length; i++) {
chunks[i] = str.slice(index, index += size)
}
return chunks
}
}
function chunkRight (str, size = 3) {
if (typeof str === 'string') {
const length = str.length
const chunks = Array(Math.ceil(length / size))
if (length) {
chunks[0] = str.slice(0, length % size || size)
for (let i = 1, index = chunks[0].length; index < length; i++) {
chunks[i] = str.slice(index, index += size)
}
}
return chunks
}
}
console.log(chunkRight()) // undefined
console.log(chunkRight('')) // []
console.log(chunkRight('1')) // ["1"]
console.log(chunkRight('123')) // ["123"]
console.log(chunkRight('1234')) // ["1", "234"]
console.log(chunkRight('12345')) // ["12", "345"]
console.log(chunkRight('123456')) // ["123", "456"]
console.log(chunkRight('1234567')) // ["1", "234", "567"]