如何拆分一个字符串,然后将返回的值再次拆分连接到另一个必须拆分的值,并将余数连接到剩余的
How to split a string and then split the returned value again connected to another value that has to be split, and join the remainder to the remaining
我想要的是一个大约有 2000 到 3000 个字符的字符串,其中有一百多个 \n
非均匀分布,我想每 1000 个字符拆分它们,然后在数组中的 returned 字符串,对于 returned 数组中的每个值,我想在最后一个 \n
结束字符串(如果字符串保持原样不包含 \n
) 并且最后一个 \n
之后的字符串的剩余部分应该附加到数组中下一个值的开头,然后应该在前一个字符串固定后执行到最后 \n
希望你明白我的意思,这是我的代码
module.exports={
async split(text,length,max){
if (text.length > max){
return;
}
let regex = new RegExp(`.{1,${length}}`, "g");
let splitted = text.match(regex);
return splitted;
}
}
获取执行的地方在这里:
let splitted = await split(lyrics,1000,6000)
我设法每 1000 个单词进行拆分,但我在顶部解释的事情是我想做的,我无法做到,所以有人可以帮忙吗?
编辑:假设我想将字符串拆分为最多 20 个字符,最大字符串长度为 1000,如果它绕过了该限制,则意味着不会 returned。它可以使用空格 (
) 进行第二阶段的拆分(正如我在问题中提到的 \n
)。
假设字符串是:Hello, I love Stack Overflow, and it is super cool
在我当前的代码中,如果我们这样做了
let string = `Hello, I love Stack Overflow, and it is super cool`
let splitted = await split(string, 10, 1000)
会return
["Hello, I l", "ove Stack ", "Overflow, ", "and it is ", "super cool"]
如果在 split()
中添加另一个参数会怎么样:
async split(text, length, max, splitAt)
splitAt
可以表示 \n
或
取决于选择
我想要return的结果是:["Hello, I", "love Stack", "Overflow,", "and it is", "super cool"]
问题是,我不明白该怎么做
如果我没理解错的话,您想将文本分成最大大小为 1000 的块,并且它们应该以换行符结尾。
function split(str, chunkSize){
const chunks = [];
let current_chunk = ""
str.split("\n").forEach(part => {
if(current_chunk.length + part.length > chunkSize){
// Adds the chunk to chunks and resets current chunk.
chunks.push(current_chunk);
current_chunk = "";
}
// adds \n to the start of the part, if the current chunk isn't empty.
current_chunk.length
? current_chunk += `\n${part}`
: current_chunk += part
})
// Used to get the last one, if it isn't empty.
if(current_chunk.length) chunks.push(current_chunk);
return chunks;
}
应该看起来像这样。不过还没有测试过,因为我已经把它写在我的 phone.
上了
您当然不需要此方法为 async
,它应该只是单步执行字符串、按 len
拆分并找到 lastIndexOf
您的 splitAt
参数,然后使用 substring
将该块放入数组中
像这样:
function split(text, len, max, splitAt) {
if (text.length > max) {
return;
}
let pos = 0;
const chunks = []
while (pos < text.length) {
if (pos + len >= text.length) {
chunks.push(text.substring(pos));
pos = text.length
} else {
const s = text.substring(pos, pos + len + 1).lastIndexOf(splitAt);
chunks.push(text.substring(pos, pos + s));
pos += s + 1;
}
}
return chunks;
}
let string = `Hello, I love Stack Overflow, and it is super cool`
let splitted = split(string, 10, 1000, " ")
console.log(splitted);
我想要的是一个大约有 2000 到 3000 个字符的字符串,其中有一百多个 \n
非均匀分布,我想每 1000 个字符拆分它们,然后在数组中的 returned 字符串,对于 returned 数组中的每个值,我想在最后一个 \n
结束字符串(如果字符串保持原样不包含 \n
) 并且最后一个 \n
之后的字符串的剩余部分应该附加到数组中下一个值的开头,然后应该在前一个字符串固定后执行到最后 \n
希望你明白我的意思,这是我的代码
module.exports={
async split(text,length,max){
if (text.length > max){
return;
}
let regex = new RegExp(`.{1,${length}}`, "g");
let splitted = text.match(regex);
return splitted;
}
}
获取执行的地方在这里:
let splitted = await split(lyrics,1000,6000)
我设法每 1000 个单词进行拆分,但我在顶部解释的事情是我想做的,我无法做到,所以有人可以帮忙吗?
编辑:假设我想将字符串拆分为最多 20 个字符,最大字符串长度为 1000,如果它绕过了该限制,则意味着不会 returned。它可以使用空格 (
) 进行第二阶段的拆分(正如我在问题中提到的 \n
)。
假设字符串是:Hello, I love Stack Overflow, and it is super cool
在我当前的代码中,如果我们这样做了
let string = `Hello, I love Stack Overflow, and it is super cool`
let splitted = await split(string, 10, 1000)
会return
["Hello, I l", "ove Stack ", "Overflow, ", "and it is ", "super cool"]
如果在 split()
中添加另一个参数会怎么样:
async split(text, length, max, splitAt)
splitAt
可以表示 \n
或
取决于选择
我想要return的结果是:["Hello, I", "love Stack", "Overflow,", "and it is", "super cool"]
问题是,我不明白该怎么做
如果我没理解错的话,您想将文本分成最大大小为 1000 的块,并且它们应该以换行符结尾。
function split(str, chunkSize){
const chunks = [];
let current_chunk = ""
str.split("\n").forEach(part => {
if(current_chunk.length + part.length > chunkSize){
// Adds the chunk to chunks and resets current chunk.
chunks.push(current_chunk);
current_chunk = "";
}
// adds \n to the start of the part, if the current chunk isn't empty.
current_chunk.length
? current_chunk += `\n${part}`
: current_chunk += part
})
// Used to get the last one, if it isn't empty.
if(current_chunk.length) chunks.push(current_chunk);
return chunks;
}
应该看起来像这样。不过还没有测试过,因为我已经把它写在我的 phone.
上了您当然不需要此方法为 async
,它应该只是单步执行字符串、按 len
拆分并找到 lastIndexOf
您的 splitAt
参数,然后使用 substring
像这样:
function split(text, len, max, splitAt) {
if (text.length > max) {
return;
}
let pos = 0;
const chunks = []
while (pos < text.length) {
if (pos + len >= text.length) {
chunks.push(text.substring(pos));
pos = text.length
} else {
const s = text.substring(pos, pos + len + 1).lastIndexOf(splitAt);
chunks.push(text.substring(pos, pos + s));
pos += s + 1;
}
}
return chunks;
}
let string = `Hello, I love Stack Overflow, and it is super cool`
let splitted = split(string, 10, 1000, " ")
console.log(splitted);