我如何把一个字符串分成n个字符的片段?

正如标题所示,我有一个字符串,我想把它分割成n字符的片段。

例如:

var str = 'abcdefghijkl';

在使用n=3进行一些魔法之后,它将变成

var arr = ['abc','def','ghi','jkl'];

有办法做到这一点吗?

250117 次浏览

如果你不想使用正则表达式…

var chunks = [];


for (var i = 0, charsLength = str.length; i < charsLength; i += 3) {
chunks.push(str.substring(i, i + 3));
}

jsFiddle

...否则正则表达式的解决方案是相当好的:)

str.match(/.{3}/g); // => ['abc', 'def', 'ghi', 'jkl']

var str = 'abcdefghijkl';
console.log(str.match(/.{1,3}/g));

对于不是3倍数的字符串长度,使用{1,3}而不是{3}来包含余数,例如:

console.log("abcd".match(/.{1,3}/g)); // ["abc", "d"]


A couple more subtleties:

  1. If your string may contain newlines (which you want to count as a character rather than splitting the string), then the . won't capture those. Use /[\s\S]{1,3}/ instead. (Thanks @Mike).
  2. If your string is empty, then match() will return null when you may be expecting an empty array. Protect against this by appending || [].

So you may end up with:

var str = 'abcdef \t\r\nghijkl';
var parts = str.match(/[\s\S]{1,3}/g) || [];
console.log(parts);


console.log(''.match(/[\s\S]{1,3}/g) || []);

基于之前对这个问题的回答;下面的函数将拆分字符串(str) n-number (size)的字符。

function chunk(str, size) {
return str.match(new RegExp('.{1,' + size + '}', 'g'));
}

演示

.
(function() {
function chunk(str, size) {
return str.match(new RegExp('.{1,' + size + '}', 'g'));
}
  

var str = 'HELLO WORLD';
println('Simple binary representation:');
println(chunk(textToBin(str), 8).join('\n'));
println('\nNow for something crazy:');
println(chunk(textToHex(str, 4), 8).map(function(h) { return '0x' + h }).join('  '));
  

// Utiliy functions, you can ignore these.
function textToBin(text) { return textToBase(text, 2, 8); }
function textToHex(t, w) { return pad(textToBase(t,16,2), roundUp(t.length, w)*2, '00'); }
function pad(val, len, chr) { return (repeat(chr, len) + val).slice(-len); }
function print(text) { document.getElementById('out').innerHTML += (text || ''); }
function println(text) { print((text || '') + '\n'); }
function repeat(chr, n) { return new Array(n + 1).join(chr); }
function textToBase(text, radix, n) {
return text.split('').reduce(function(result, chr) {
return result + pad(chr.charCodeAt(0).toString(radix), n, '0');
}, '');
}
function roundUp(numToRound, multiple) {
if (multiple === 0) return numToRound;
var remainder = numToRound % multiple;
return remainder === 0 ? numToRound : numToRound + multiple - remainder;
}
}());
#out {
white-space: pre;
font-size: 0.8em;
}
<div id="out"></div>

function chunk(er){
return er.match(/.{1,75}/g).join('\n');
}

上面的函数是我用于Base64分块的。它将创建一个每75个字符的换行符。

一些不使用正则表达式的干净解决方案:

/**
* Create array with maximum chunk length = maxPartSize
* It work safe also for shorter strings than part size
**/
function convertStringToArray(str, maxPartSize){


const chunkArr = [];
let leftStr = str;
do {


chunkArr.push(leftStr.substring(0, maxPartSize));
leftStr = leftStr.substring(maxPartSize, leftStr.length);


} while (leftStr.length > 0);


return chunkArr;
};

使用示例—https://jsfiddle.net/maciejsikora/b6xppj4q/.使用实例

我还尝试将我的解决方案与被选为正确答案的regexp进行比较。可以在jsfiddle - https://jsfiddle.net/maciejsikora/2envahrk/上找到一些测试。测试表明这两种方法具有相似的性能,可能乍一看regexp解决方案稍微快一点,但请自行判断。

我的解决方案(ES6语法):

const source = "8d7f66a9273fc766cd66d1d";
const target = [];
for (
const array = Array.from(source);
array.length;
target.push(array.splice(0,2).join(''), 2));

我们甚至可以这样创建一个函数:

function splitStringBySegmentLength(source, segmentLength) {
if (!segmentLength || segmentLength < 1) throw Error('Segment length must be defined and greater than/equal to 1');
const target = [];
for (
const array = Array.from(source);
array.length;
target.push(array.splice(0,segmentLength).join('')));
return target;
}

然后你可以以一种可重用的方式轻松地调用函数:

const source = "8d7f66a9273fc766cd66d1d";
const target = splitStringBySegmentLength(source, 2);

干杯

这里我们每隔n个字符就在一个字符串中穿插另一个字符串:

export const intersperseString = (n: number, intersperseWith: string, str: string): string => {


let ret = str.slice(0,n), remaining = str;


while (remaining) {
let v = remaining.slice(0, n);
remaining = remaining.slice(v.length);
ret += intersperseWith + v;
}


return ret;


};

如果我们像这样使用上面的语句:

console.log(splitString(3,'|', 'aagaegeage'));

我们得到:

亚美大陆煤层气有限公司|亚美大陆煤层气有限公司| aeg |坚毅不屈| e

这里我们做同样的事情,但是push到一个数组:

export const sperseString = (n: number, str: string): Array<string> => {


let ret = [], remaining = str;


while (remaining) {
let v = remaining.slice(0, n);
remaining = remaining.slice(v.length);
ret.push(v);
}


return ret;


};

然后运行它:

console.log(sperseString(5, 'foobarbaztruck'));

我们得到:

['fooba', 'rbazt', 'ruck']

如果有人知道简化上述代码的方法,请使用lmk,但它应该适用于字符串。

如果你真的需要坚持.split和/或.raplace,那么使用/(?<=^(?:.{3})+)(?!$)/g

.split:

var arr = str.split( /(?<=^(?:.{3})+)(?!$)/ )
// [ 'abc', 'def', 'ghi', 'jkl' ]

.replace:

var replaced = str.replace( /(?<=^(?:.{3})+)(?!$)/g, ' || ' )
// 'abc || def || ghi || jkl'

< br > < br >

/(?!$)/表示在字符串结束时不停止。没有它的:

var arr = str.split( /(?<=^(?:.{3})+)/ )
// [ 'abc', 'def', 'ghi', 'jkl' ] // is fine
var replaced = str.replace( /(?<=^(.{3})+)/g, ' || ')
// 'abc || def || ghi || jkl || ' // not fine

忽略组/(?:)/是为了防止数组中的重复项。没有它的:

var arr = str.split( /(?<=^(.{3})+)(?!$)/ )
// [ 'abc', 'abc', 'def', 'abc', 'ghi', 'abc', 'jkl' ] // not fine
var replaced = str.replace( /(?<=^(.{3})+)(?!$)/g, ' || ' )
// 'abc || def || ghi || jkl' // is fine
const chunkStr = (str, n, acc) => {
if (str.length === 0) {
return acc
} else {
acc.push(str.substring(0, n));
return chunkStr(str.substring(n), n, acc);
}
}
const str = 'abcdefghijkl';
const splittedString = chunkStr(str, 3, []);

干净的解决方案没有REGEX

这里有一种不需要正则表达式或显式循环的方法,尽管它有点扩展了一行代码的定义:

const input = 'abcdefghijlkm';
    

// Change `3` to the desired split length.
const output = input.split('').reduce((s, c) => {
let l = s.length-1;
(s[l] && s[l].length < 3) ? s[l] += c : s.push(c);
return s;
}, []);


console.log(output);  // output: [ 'abc', 'def', 'ghi', 'jlk', 'm' ]

它的工作原理是将字符串分割为单个字符的数组,然后使用Array.reduce遍历每个字符。通常reduce将返回单个值,但在这种情况下,单个值恰好是一个数组,当我们传递每个字符时,我们将它附加到该数组的最后一项。一旦数组中的最后一项达到目标长度,就追加一个新的数组项。

稍后再讨论,但这里有一个变化,比子字符串+数组push 1快一点。

// substring + array push + end precalc
var chunks = [];


for (var i = 0, e = 3, charsLength = str.length; i < charsLength; i += 3, e += 3) {
chunks.push(str.substring(i, e));
}

作为for循环的一部分,预先计算结束值比在子字符串中进行内联计算要快。我已经在Firefox和Chrome上测试过了,它们都显示了加速。

你可以试试

    var b1 = "";
function myFunction(n) {
if(str.length>=3){
var a = str.substring(0,n);
b1 += a+ "\n"
str = str.substring(n,str.length)
myFunction(n)
}
else{
if(str.length>0){
b1 += str
}
console.log(b1)
}
}
myFunction(4)

试试这个简单的代码,它会像魔法一样工作!

let letters = "abcabcabcabcabc";
// we defined our variable or the name whatever
let a = -3;
let finalArray = [];
for (let i = 0; i <= letters.length; i += 3) {
finalArray.push(letters.slice(a, i));
a += 3;
}
// we did the shift method cause the first element in the array will be just a string "" so we removed it
finalArray.shift();
// here the final result
console.log(finalArray);

我最喜欢的答案是gouder hicham的。但我稍微修改了一下,让它更有意义。

let myString = "Able was I ere I saw elba";


let splitString = [];
for (let i = 0; i < myString.length; i = i + 3) {
splitString.push(myString.slice(i, i + 3));
}


console.log(splitString);

下面是该代码的一个功能化版本。


function stringSplitter(myString, chunkSize) {
let splitString = [];
for (let i = 0; i < myString.length; i = i + chunkSize) {
splitString.push(myString.slice(i, i + chunkSize));
}
return splitString;
}

以及函数的用法:

let myString = "Able was I ere I saw elba";
let mySplitString = stringSplitter(myString, 3);
console.log(mySplitString);

结果是:

>(9) ['Abl', 'e w', 'as ', 'I e', 're ', 'I s', 'aw ', 'elb', 'a']
function str_split(string, length = 1) {
if (0 >= length)
length = 1;
    

if (length == 1)
return string.split('');


var string_size = string.length;
var result = [];


for (let i = 0; i < string_size / length; i++)
result[i] = string.substr(i * length, length);


return result;
}


str_split(str, 3)

Results (Chrome 104) .