将字符串分割成一定长度的部分。

时间:2022-11-03 16:19:38

I want to split a string into parts with a defined length. This means if I have a given string like "1234567890" and want to split it in parts with length 3 then I expect the result ["123", "456", "789", "0"]. To achive this I have found a solution here: https://*.com/a/14349616/2577116

我想将一个字符串分割成具有定义长度的部分。这意味着如果我有一个给定的字符串,比如“1234567890”,并且想要将它分割成长度为3的部分,那么我希望得到的结果是[123]、[456]、[789]、[0]。为了实现这个目标,我在这里找到了一个解决方案:https://*.com/a/14349616/2577116

Now, I also want to split not starting at the beginning but at the end of the string. The expected result would be ["1", "234", "567", "890"].

现在,我也想分开不是从开始而是在弦的末端。预期的结果将是["1"、"234"、"567"、"890"]。

Therefore I used and modified the solution above and came up with this:

因此我使用并修改了上面的解决方案,得出如下结论:

function (str, len, reversed) {
        //create array from string
    var _parts = str.split(""),
        _size = Math.ceil(_parts.length/len),
        _ret  = [],
        _offset;

    //should give ["123", "456", "789", "0"]
    if (!reversed) {
        for (var _i=0; _i<_size; _i++) {
            _offset = _i * len;
            _ret[_i] = _parts.slice(_offset, _offset+len).join("");
        }
    }
    //should give ["1", "234", "567", "890"]
    else {
        //reverse input
        _parts.reverse();
        //apply same algorithm as above but don't join yet
        for (var _j=0; _j<_size; _j++) {
            _offset = _j * len;
            _ret[_j] = _parts.slice(_offset, _offset+len);
        }
        //bring each subitem back to right order, join
        _ret.forEach(function (item, i) {
            _ret[i] = item.reverse().join("");
        });
        //reorder items
        _ret.reverse();
    }

    return _ret;
}

This seems to work pretty well. I'm asking for some better/simplified solution as mine feels a little bulky.

这似乎很有效。我想要一些更好的/简化的解决方案,因为我的方案有点笨重。

6 个解决方案

#1


2  

I would do something like this:

我会这样做:

    function foo(str,len,rev) {
      var result = [];
      if (rev && str.length%len != 0) {
        result.push(str.substr(0, str.length % len));
        str=str.substr(str.length % len);
      }

      for (var i=0; i<str.length; i+=len) {
        result.push(str.substr(i,len));
      }
      return result;
    }

The if statement will first check if reversed is true and if so it will calculate the rest when dividing the length of the string with the chunk size (check the modulo operation %). Then using the function "substr", that takes a starting position and a length, to get the first element of the list.

if语句将首先检查反转是否为真,如果为真,它将在用块大小分割字符串长度时计算其余部分(检查modulo operation %)。然后使用函数“substr”(取起始位置和长度)获取列表的第一个元素。

The for loop will iterate through the string "len" characters at the time and again using the function "substr" cut the string in to pieces of length "len" and add them to the list result.

for循环将遍历字符串“len”字符,并再次使用函数“substr”将字符串切割成长度为“len”的片段,并将它们添加到列表结果中。

#2


1  

You can do this by rearranging the string and Using the same chunkString function that you pointed at.

您可以通过重新排列字符串并使用您所指向的chunkString函数来实现这一点。

> chunkString(s.split('').reverse().join(''), 3).map(function(v){return v.split('').reverse().join('');}).reverse()
< ["1", "234", "567", "890"]

The split, reverse, join are used to convert string to list, reverse, and then convert back to string.
Readeable code -

分割、反向、联接用于将字符串转换为列表、反向,然后再转换为字符串。Readeable代码-

chunkString(
    s.split('').reverse().join(''),
    3
).map(
    function(v){
        return v.split('').reverse().join('');
    }
).reverse()

#3


1  

If you prefer RegEx:

如果你喜欢正则表达式:

"1234567890".match(/(.{1,3})|(.{1,})/g)

Output:

输出:

["123", "456", "789", "0"]

For Reverse:

反向:

var splitLength = 3
var _str = "1234567890"
var startSubStringLength = _str.length % splitLength

_str.match(new RegExp("(^.{1," + startSubStringLength + "})|(.{1,3})|(.{1,})", "g"))

Output:

输出:

["1", "234", "567", "890"]

Complete Function

完整的功能

var _mySplit = function(str, splitLength, doReverse) {
    var _regEx = new RegExp("(.{" + splitLength + "})|(.{1,})", "g");
    if(doReverse) {
        var startSubStringLength = str.length % splitLength
        if(startSubStringLength > 0) {
            _regEx = new RegExp("(^.{1," + startSubStringLength + "})|(.{1," + splitLength +  "})|(.{1,})", "g")
        }
    }
    return str.match(_regEx)
}

Output:

输出:

_mySplit("1234", 3, false)
["123", "4"]
_mySplit("1234", 3, true)
["1", "234"]
_mySplit("1234567890", 3, true)
["1", "234", "567", "890"]
_mySplit("1234567890", 3, false)
["123", "456", "789", "0"]

#4


1  

Try this:

试试这个:

Steps:

步骤:

  1. Reverse the string
  2. 反向的字符串
  3. Split it into group of three characters
  4. 把它分成三个字符
  5. Reverse the string
  6. 反向的字符串

Code:

代码:

var str = '1234567890';
var rev = str.split('').reverse().join('');

var matches = rev.match(/([0-9]{1,3})/g).reverse().map(function(el) {
  return el.split('').reverse().join('');
});
console.log(matches);

Even shorter(Less Redable)

更短(Redable少)

var matches = '1234567890'.split('').reverse().join('').match(/([0-9]{1,3})/g).reverse().map(function(el) {
  return el.split('').reverse().join('');
});
console.log(matches);

#5


0  

My solution :

我的解决方案:

function mySplit (str, len, reversed) {
        //create array from string
  var _parts = str.split(""),
      _size = Math.ceil(_parts.length/len),
      _ret  = [],
      _offset;


  for (var _i=0; _i<_size; _i++) {
    _offset = Math.abs((reversed ? _parts.length : 0 ) - (_i * len));
    var sliceStart = reversed ? Math.max(0, _offset - len) : _offset;
    var sliceEnd = reversed ? _offset : _offset + len;
    _ret.push(_parts.slice(sliceStart, sliceEnd).join(""));
  }
  reversed && _ret.reverse();


  return _ret;
}

var log = document.getElementById('log');

log.innerHTML += "string : 1234567890";
log.innerHTML += "\nreversed:" + mySplit("1234567890", 3, true)
log.innerHTML += "\nnot reversed:" + mySplit("1234567890", 3, false)

log.innerHTML += "\n\nstring : 123456789";
log.innerHTML += "\nreversed:" + mySplit("123456789", 3, true)
log.innerHTML += "\nnot reversed:" + mySplit("123456789", 3, false)
<pre id="log"></pre>

#6


0  

This is what i made.

这是我做的。

var string = '1234567890';
var chunks = 3;

console.log( sliceIt(string, chunks, true) );
console.log( sliceIt(string, chunks, false) );



function sliceIt( str , steps , rev ) {

  var iterations  = Math.ceil(str.length); // for the loop
  var returnArray = [];
  var strLen      = str.length;
  var modulo      = strLen % steps; // for reverse purposes

  // if modulo is 0 then no action needed. returns the modulo if both true
  var firstStep       = (rev === true) && (modulo != 0) ? modulo : false;  

  for (i=0; i < iterations;) {        

      // pushing to array
      returnArray.push(str.substr( i , firstStep || steps ));   

      // increment the right way 
      i+= firstStep || steps; 

      // first step done.. deactivate
      firstStep = false; 
    }
    return returnArray;
  }

#1


2  

I would do something like this:

我会这样做:

    function foo(str,len,rev) {
      var result = [];
      if (rev && str.length%len != 0) {
        result.push(str.substr(0, str.length % len));
        str=str.substr(str.length % len);
      }

      for (var i=0; i<str.length; i+=len) {
        result.push(str.substr(i,len));
      }
      return result;
    }

The if statement will first check if reversed is true and if so it will calculate the rest when dividing the length of the string with the chunk size (check the modulo operation %). Then using the function "substr", that takes a starting position and a length, to get the first element of the list.

if语句将首先检查反转是否为真,如果为真,它将在用块大小分割字符串长度时计算其余部分(检查modulo operation %)。然后使用函数“substr”(取起始位置和长度)获取列表的第一个元素。

The for loop will iterate through the string "len" characters at the time and again using the function "substr" cut the string in to pieces of length "len" and add them to the list result.

for循环将遍历字符串“len”字符,并再次使用函数“substr”将字符串切割成长度为“len”的片段,并将它们添加到列表结果中。

#2


1  

You can do this by rearranging the string and Using the same chunkString function that you pointed at.

您可以通过重新排列字符串并使用您所指向的chunkString函数来实现这一点。

> chunkString(s.split('').reverse().join(''), 3).map(function(v){return v.split('').reverse().join('');}).reverse()
< ["1", "234", "567", "890"]

The split, reverse, join are used to convert string to list, reverse, and then convert back to string.
Readeable code -

分割、反向、联接用于将字符串转换为列表、反向,然后再转换为字符串。Readeable代码-

chunkString(
    s.split('').reverse().join(''),
    3
).map(
    function(v){
        return v.split('').reverse().join('');
    }
).reverse()

#3


1  

If you prefer RegEx:

如果你喜欢正则表达式:

"1234567890".match(/(.{1,3})|(.{1,})/g)

Output:

输出:

["123", "456", "789", "0"]

For Reverse:

反向:

var splitLength = 3
var _str = "1234567890"
var startSubStringLength = _str.length % splitLength

_str.match(new RegExp("(^.{1," + startSubStringLength + "})|(.{1,3})|(.{1,})", "g"))

Output:

输出:

["1", "234", "567", "890"]

Complete Function

完整的功能

var _mySplit = function(str, splitLength, doReverse) {
    var _regEx = new RegExp("(.{" + splitLength + "})|(.{1,})", "g");
    if(doReverse) {
        var startSubStringLength = str.length % splitLength
        if(startSubStringLength > 0) {
            _regEx = new RegExp("(^.{1," + startSubStringLength + "})|(.{1," + splitLength +  "})|(.{1,})", "g")
        }
    }
    return str.match(_regEx)
}

Output:

输出:

_mySplit("1234", 3, false)
["123", "4"]
_mySplit("1234", 3, true)
["1", "234"]
_mySplit("1234567890", 3, true)
["1", "234", "567", "890"]
_mySplit("1234567890", 3, false)
["123", "456", "789", "0"]

#4


1  

Try this:

试试这个:

Steps:

步骤:

  1. Reverse the string
  2. 反向的字符串
  3. Split it into group of three characters
  4. 把它分成三个字符
  5. Reverse the string
  6. 反向的字符串

Code:

代码:

var str = '1234567890';
var rev = str.split('').reverse().join('');

var matches = rev.match(/([0-9]{1,3})/g).reverse().map(function(el) {
  return el.split('').reverse().join('');
});
console.log(matches);

Even shorter(Less Redable)

更短(Redable少)

var matches = '1234567890'.split('').reverse().join('').match(/([0-9]{1,3})/g).reverse().map(function(el) {
  return el.split('').reverse().join('');
});
console.log(matches);

#5


0  

My solution :

我的解决方案:

function mySplit (str, len, reversed) {
        //create array from string
  var _parts = str.split(""),
      _size = Math.ceil(_parts.length/len),
      _ret  = [],
      _offset;


  for (var _i=0; _i<_size; _i++) {
    _offset = Math.abs((reversed ? _parts.length : 0 ) - (_i * len));
    var sliceStart = reversed ? Math.max(0, _offset - len) : _offset;
    var sliceEnd = reversed ? _offset : _offset + len;
    _ret.push(_parts.slice(sliceStart, sliceEnd).join(""));
  }
  reversed && _ret.reverse();


  return _ret;
}

var log = document.getElementById('log');

log.innerHTML += "string : 1234567890";
log.innerHTML += "\nreversed:" + mySplit("1234567890", 3, true)
log.innerHTML += "\nnot reversed:" + mySplit("1234567890", 3, false)

log.innerHTML += "\n\nstring : 123456789";
log.innerHTML += "\nreversed:" + mySplit("123456789", 3, true)
log.innerHTML += "\nnot reversed:" + mySplit("123456789", 3, false)
<pre id="log"></pre>

#6


0  

This is what i made.

这是我做的。

var string = '1234567890';
var chunks = 3;

console.log( sliceIt(string, chunks, true) );
console.log( sliceIt(string, chunks, false) );



function sliceIt( str , steps , rev ) {

  var iterations  = Math.ceil(str.length); // for the loop
  var returnArray = [];
  var strLen      = str.length;
  var modulo      = strLen % steps; // for reverse purposes

  // if modulo is 0 then no action needed. returns the modulo if both true
  var firstStep       = (rev === true) && (modulo != 0) ? modulo : false;  

  for (i=0; i < iterations;) {        

      // pushing to array
      returnArray.push(str.substr( i , firstStep || steps ));   

      // increment the right way 
      i+= firstStep || steps; 

      // first step done.. deactivate
      firstStep = false; 
    }
    return returnArray;
  }