JavaScript按逗号和字符计数拆分字符串

时间:2017-02-03 15:45:32

标签: javascript jquery arrays string split

我有一个包含大约10,000个逗号分隔符的字符串,我想用逗号分隔它们,并确保每个字符数不超过1500个字符。

例如:

var str = "12345, 567789, 435345, 55446444564, 786756, 33322 .... ";
var commaSeperatedSplitArr = str.split(",");
var arrayOfArrays = [];
var limit = 50;

// So, I would like to get my commaSeperatedSplitArr[0].length >= 1500 and <= 1600 characters such that none of the Ids are cropped off    
while(commaSeperatedSplitArr.length > 0) {
    var splicedCharCount = arrayOfEmails.splice(0, limit)[0].length;
    if(splicedCharCount >= 1500 && splicedCharCount <= 1600) {
        arrayOfArrays.push(commaSeperatedSplitArr.splice(0, limit));  
    }
}

实现这一目标的最佳方法是什么?我尝试过使用str.splice(0,limit),但这只是给了我一个限制数而不是我想要的字符数。

更新

另外,我们如何使用上述方法申请电子邮件地址列表?

For E.g.: var str = "abc@gmail.com, xyz@help-desk.com, hello@world.com.... "

预期结果 1)对于Ids :(如果限制为50个字符)

arrayOfArrays[0] = "12345, 567789, ... "; (should contain 50 characters or less without any id being cropped off.)

2)对于电子邮件地址:(如果限制为50个字符)

arrayofArrays[0] = "abc@gmail.com, xyz@help-desk.com,..."; (should contain 50 characters or less without any email address being cropped off.)

期待您的解决方案。 欢呼声。

2 个答案:

答案 0 :(得分:1)

尝试以下几点:

&#13;
&#13;
var str = "12345, 567789, 435345, 55446444564, 786756, 33322";
var commaSeparatedSplitArr = str.split(", ");
var arrayOfArrays = [];
var currentArrayCharCount = 0;
var currentArray = [];
var arrayCharCountLimit = 10;
for (var i = 0; i < commaSeparatedSplitArr.length; i++) {
  var currentId = commaSeparatedSplitArr[i];
  if (currentArrayCharCount + currentId.length > arrayCharCountLimit) {
    arrayOfArrays.push(currentArray);
    currentArray = [];
    currentArrayCharCount = 0;
    if (currentId.length <= arrayCharCountLimit) {
      currentArray.push(currentId);
      currentArrayCharCount += currentId.length;
    } else {
      console.error("ID: " + currentId + " is too long!");
    }
  } else {
    currentArray.push(currentId);
    currentArrayCharCount += currentId.length;
  }
}
arrayOfArrays.push(currentArray);
console.log(arrayOfArrays);
&#13;
&#13;
&#13;

此外,如果字符串中确实存在空格,您可能需要考虑将分割字符更改为", "(包括空格),以便ids不会在开始。

编辑:进行了一些更正并转换为堆叠代码段。

答案 1 :(得分:1)

使用正则表达式的单线解决方案。

它使用管道更改字符串的限制字符之前的最后一个昏迷,一直这样做直到字符串结束。然后在管道上分裂。

&#13;
&#13;
var str = "12345, 567789, 435345, 55446444564, 786756, 33322, 12345, 567789, 435345, 55446444564, 786756, 33322, 12345, 567789, 435345, 55446444564, 786756, 33322"
var limit = 20;

var regex = new RegExp(`([^]{1,${limit}})(, |$)`, "g");
var out = str.replace(regex, "|$1").slice(1).split("|")

console.log(out.map(l => `"${l}"\t\t(size: ${l.length})`).join("\n"))
&#13;
&#13;
&#13;

修改

这里是没有模板文字支持旧版浏览器的示例

&#13;
&#13;
var str = "12345, 567789, 435345, 55446444564, 786756, 33322, 12345, 567789, 435345, 55446444564, 786756, 33322, 12345, 567789, 435345, 55446444564, 786756, 33322"
    var limit = 20;

    var regex = new RegExp('([^]{1,' + limit + '})(, |$)', "g");
    var out = str.replace(regex, "|$1").slice(1).split("|")

    console.log(out.map(l => '"'+l+'"\t\t(size: '+l.length+')').join("\n"))
&#13;
&#13;
&#13;