简体   繁体   English

我如何切片文本块,使其不以拆分词结尾?

[英]How can I slice a block of text so that it doesn't end on a split word?

I've written code that takes a large block of text, splits it into 995 character blocks and pushes each block into an array. 我编写的代码需要一个大文本块,将其拆分为995个字符块,并将每个块压入一个数组。 However, this often results splits words when they fall at the 995-character line- how could I edit my code so that each block of text is as close as possible to 995 characters long (must be under) but ends at the last available space? 但是,当它们落在995个字符的行时,这通常会导致单词分裂-我该如何编辑代码,以使每个文本块都尽可能接近995个字符长(必须在下面),但在最后一个可用空格处结束?

function cutUp() {
var PAname = prompt("Determine PA type and PA Name (e.g. 5=mexican food=");
var chunks = [];
var OGstring = document.getElementById('PATypes').value;
var my_long_string = OGstring.split('1').join('').split('2').join('').split('3').join('').split('4').join('').split('5').join('').split('6').join('').split('7').join('').split('8').join('').split('9').join('').split('0').join('').split('[edit]').join('').split('[citation needed]').join('').split('[').join('').split(']').join('').split('(').join('').split(')').join('');



var i = 0;
var n = 0;
while (n < my_long_string.length) {
    chunks.push(my_long_string.slice(n, n += 995));

}
if (chunks[0] != null) {
    $('PAType=Name=Value8').innerHTML = PAname + chunks[0];
}
if (chunks[1] != null) {
    $('PAType=Name=Value9').innerHTML = PAname + chunks[1];
}
if (chunks[2] != null) {
    $('PAType=Name=Value10').innerHTML = PAname + chunks[2];
}
if (chunks[3] != null) {
    $('PAType=Name=Value11').innerHTML = PAname + chunks[3];
}
if (chunks[4] != null) {
    $('PAType=Name=Value12').innerHTML = PAname + chunks[4];
}
if (chunks[5] != null) {
    $('PAType=Name=Value13').innerHTML = PAname + chunks[5];
}
if (chunks[6] != null) {
    $('PAType=Name=Value14').innerHTML = PAname + chunks[6];
}
if (chunks[7] != null) {
    $('PAType=Name=Value15').innerHTML = PAname + chunks[7];
}
if (chunks[8] != null) {
    $('PAType=Name=Value16').innerHTML = PAname + chunks[8];
}
if (chunks[9] != null) {
    $('PAType=Name=Value17').innerHTML = PAname + chunks[9];
}

////this is to create new exportable table
$('exportTable').innerHTML += $('tableContents').innerHTML;
$("exportTable").removeClass('hidden');
///this resets to default
defaultReset();

}​ }

Without any specific Javascript knowledge, I'd say to look ahead 995 characters, and if that character isn't itself a space, to start looking back towards the start until you find a space, and truncate there. 在没有任何特定Javascript知识的情况下,我想先检查995个字符,如果该字符本身不是空格,则应从头开始回头,直到找到一个空格,然后在该处截断。

In C-like pseudocode, with chars being your big array of characters: 在类似C的伪代码中, charschars的大数组:

for(truncOffset = 995; truncOffset > 0; truncOffset--):
{  
    if chars[currentLoc + truncOffset] = ' ': /* a space character */ break;  
}

Rinse, repeat to taste. 冲洗,重复品尝。 Remember to move currentLoc to currentLoc + truncOffset after finding that space. 记住在找到该空间后将currentLoc移到currentLoc + truncOffset

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择文本块每一行的第一个单词? - How can I select the first word of every line of a block of text? 如何使之双击div时不选择单词? - How do I make it so that when I double click on a div, it doesn't select the word? 如何包含iframe视频,以使其不会加载 - How can I contain an iframe video so that it doesn't load 尽管包括数字的长度,但为什么它不将数字切到底 - How come it doesn't slice a number until the end despite including the number's length 如何在移动设备上结束当前的预想单词? - How do I end the current predictive text word on mobile? 如何在High Charts PIE数据中添加自定义颜色| 切片并想要更改切片文本 - How can I put custom color in High Charts PIE data | Slice and want to change slice text 如何创建一个空的HTML锚点,以便在单击它时页面不会“跳起来”? - How can I create an empty HTML anchor so the page doesn't “jump up” when I click it? 如何在javascript中将文本拆分 - How can I split text in parts in javascript 使用Javascript或CSS,如何在文本块的末尾添加额外的空间,但前提是它不能以新行开头? - Using Javascript or CSS, how can I add additional space at the end of a block of text, but only if it does not start on a new line? 如何从数组中选择一个随机对象,然后将其删除,以免重复出现? - How can I select a random object from an array and then remove it so it doesn't get repeated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM