将数组中的字符串组合到一定长度 [英] Combining strings in an array upto certain length

查看:86
本文介绍了将数组中的字符串组合到一定长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var chars = 100;

var s = [
"when an unknown printer took a galley of type and scrambled it to make a type specimen book",  //contains 91 chars
"essentially unchanged. It was popularised in the 1960s with the release",          //contains 71 chars
"unchanged essentially. popularised It was in the 1960s with the release",          //contains 71 chars
"It is a long established", //contains 24 chars
"search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years",    //contains 121 chars
"injected humour and the like"          //contains 28 chars
]

我想加入(通过\ n)如果当前句子中的字符数少于变量 chars = 100

I want to join (by \n) the next sentence if the number of characters in the present sentence is LESS than the variable chars=100

如果 chars = 100

1) s [ 0] 小于100,因此我必须加入 s [1] s [1]

1) s[0] is less than 100 so I will have to join s[1] and s[1]

2) s [2] 小于100,所以我必须加入 s [3] ,但合并后仍然是95,因此我需要进一步加入 s [4]

2) s[2] is less than 100 so I will have to join s[3] but still after combining they are 95 hence I need to further join s[4]

3)显示 s [5] ,因为列表为空

3) display s[5] as the list is empty

期望输出:

1)当一个未知的打印机拿起一个类型的厨房并将其打乱以使一本类型的样本书
基本不变。它在1960年代得到了普及,发行版本

1) when an unknown printer took a galley of type and scrambled it to make a type specimen book essentially unchanged. It was popularised in the 1960s with the release

2)基本保持不变。流行于1960年代,发布了
。这是一个由来已久的
搜索 lorem ipsum,它将发现许多仍处于起步阶段的网站。多年来,各种版本都有发展。

2) unchanged essentially. popularised It was in the 1960s with the release It is a long established search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years

3)注入幽默之类的东西

3) injected humour and the like

如何在JS中实现

var x = "";
var y = [];
for (var i = 0; i < s.length; i++) {
 if(x.length<100)
 {
    x=x+s[i];
    continue;
 }
y.push(x)
x="";

}

y.push(x)
console.log(y.join("\n\n"));


推荐答案

一种方法是仅对数组进行一次解析但使用另一个数组作为结果:

One way of doing it by parsing the array only once but using another array for the result:

var chars = 100;

var s = [
    "when an unknown printer took a galley of type and scrambled it to make a type specimen book",
    "essentially unchanged. It was popularised in the 1960s with the release",
    "unchanged essentially. popularised It was in the 1960s with the release", //contains 71 chars
    "It is a long established", //contains 24 chars
    "search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years", //contains 121 chars
    "injected humour and the like" //contains 28 chars
  ],
  out = [],
  tmp;

s.forEach((str, index) => {
  tmp = tmp ? tmp + '\n' + str : str;
  if (tmp.length > chars || index == s.length - 1) {
    out.push(tmp);
    tmp = null;
  }
});

console.log(out.join('\n\n'));

这篇关于将数组中的字符串组合到一定长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆