我正在创建一个程序,该程序将在x个字符处分割行,并且仅在空格(" ")处进行分割。
输入段落:
现在我得到的输出是这样的:
我尝试了这段代码:
输入段落:
输入分割字符:30那天没有散步的可能性。事实上,我们早上在没有叶子的灌木丛中漫步了一个小时;但自午餐以来(里德夫人在没有客人时提前用餐),寒冷的冬风带来了如此阴沉的云和雨
现在我得到的输出是这样的:
var textArray = ["There was no possibility of ta","king a walk that day. We had b","een wandering, indeed, in the ","leafless shrubbery an hour in ","the morning; but since dinner ","(Mrs. Reed, when there was no ","company, dined early) the cold"," winter wind had brought with ","it clouds so sombre, and a rai","n so "]`
但我只想在空格处进行分割 (). 它会在指定字符数之前的最后一个空格处进行分割。
我希望输出结果如下:
var textArray = ["There was no possibility of", "taking a walk that day. We", "had been wandering, indeed, in", "the leafless shrubbery an hour", "in the morning; but since", "dinner (Mrs. Reed, when there", "was no company, dined early)", "the cold winter wind had", "brought with it clouds so", "sombre, and a rain so", "penetrating, that further", "out-door exercise was now out", "of the question."]`
我尝试了这段代码:
我尝试了这段代码:
function textToArray() {
var str = document.getElementById('stringg').value;
var strArr = [];
var count = parseInt(document.getElementById('numberOfWords').value, 10) || 1;
for (var i = 0; i < str.length; i = i + count) {
var j = i + count;
strArr.push(str.substring(i,j));
}
}