使用 JavaScript 在每个第 n 个位置拆分一个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12686746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:01:21  来源:igfitidea点击:

Split a string, at every nth position, with JavaScript?

javascriptstring

提问by Lucas

I have the following string:

我有以下字符串:

foofaafoofaafoofaafoofaafoofaa

An array with 10 rows (if I split by every 3rd character, that is), which looks something like this, if I were to instantiate it:

一个有 10 行的数组(如果我按每个第三个字符分割),它看起来像这样,如果我要实例化它:

var fooarray = new Array ('foo', 'faa', 'foo', 'faa', 'foo', 'faa', 'foo', 'faa', 'foo', 'faa');

So I want a function, either built-in or custom-made, which can help me split up a string by every nth character.

所以我想要一个函数,无论是内置的还是定制的,它可以帮助我按每个第 n 个字符拆分一个字符串。

回答by xdazz

Try the below code:

试试下面的代码:

var foo = "foofaafoofaafoofaafoofaafoofaa";
console.log( foo.match(/.{1,3}/g) );

For nth position:

对于第 n 个位置:

foo.match(new RegExp('.{1,' + n + '}', 'g'));

回答by sp00m

var s = "foofaafoofaafoofaafoofaafoofaa";
var a = [];
var i = 3;

do{ a.push(s.substring(0, i)) } 
while( (s = s.substring(i, s.length)) != "" );

console.log( a )

Prints:

印刷:

foo,faa,foo,faa,foo,faa,foo,faa,foo,faa

Working demo: http://jsfiddle.net/9RXTW/

工作演示:http: //jsfiddle.net/9RXTW/

回答by freefaller

As I was writing this, @xdazz came up with the wonderfully simple regex solution.

在我写这篇文章的时候,@xdazz 想出了一个非常简单的正则表达式解决方案。

As you have asked (om the comments to that answer) for a non-regex solution, I will submit this anyway...

正如您所要求的(对该答案的评论)非正则表达式解决方案,无论如何我都会提交...

function splitNChars(txt, num) {
  var result = [];
  for (var i = 0; i < txt.length; i += num) {
    result.push(txt.substr(i, num));
  }
  return result;
}

splitNChars("foofaafoofaafoofaafoofaafoofaa",3);

JSFiddle Demo

JSFiddle 演示

回答by Guffa

You can do like this:

你可以这样做:

var input = "foofaafoofaafoofaafoofaafoofaa";

var result = [];
while (input.length) {
    result.push(input.substr(0, 3));
    input = input.substr(3);
}

Demo: http://jsfiddle.net/Guffa/yAZJ2/

演示:http: //jsfiddle.net/Guffa/yAZJ2/

回答by Lucas

As Mark Walter has pointed out, this solution from another Stack Overflow questionworks perfectly:

正如 Mark Walter 所指出的,这个来自另一个 Stack Overflow 问题的解决方案非常有效:

function splitStringAtInterval (string, interval) {
  var result = [];
  for (var i=0; i<string.length; i+=interval)
    result.push(string.substring (i, i+interval));
  return result;
}

回答by sarahg

The function followed by an example using it. The example test outputs: ["abc","def","ghi","j"]

该函数后面是一个使用它的例子。示例测试输出:["abc","def","ghi","j"]

function splitEvery(str, n)
{
    var arr = new Array;
    for (var i = 0; i < str.length; i += n)
    {
        arr.push(str.substr(i, n));
    }
    return arr;
}

var result = splitEvery('abcdefghij', 3);
document.write(JSON.stringify(result));