java - 根据长度拆分并添加字符串

标签 java string list stringbuilder

我有一个段落作为输入字符串。我试图将段落拆分为句子数组,其中每个元素包含的确切句子不超过 250 个字符。

我尝试根据分隔符分割字符串 (as .) 。将所有字符串转换为列表。使用 StringBuilder ,我尝试根据长度(250 个字符)附加字符串。

    List<String> list = new ArrayList<String>();

    String text = "Perhaps far exposed age effects. Now distrusts you her delivered applauded affection out sincerity. As tolerably recommend shameless unfeeling he objection consisted. She although cheerful perceive screened throwing met not eat distance. Viewing hastily or written dearest elderly up weather it as. So direction so sweetness or extremity at daughters. Provided put unpacked now but bringing. Unpleasant astonished an diminution up partiality. Noisy an their of meant. Death means up civil do an offer wound of. Called square an in afraid direct. Resolution diminution conviction so mr at unpleasing simplicity no. No it as breakfast up conveying earnestly immediate principle. Him son disposed produced humoured overcame she bachelor improved. Studied however out wishing but inhabit fortune windows. ";

    Pattern re = Pattern.compile("[^.!?\\s][^.!?]*(?:[.!?](?!['\"]?\\s|$)[^.!?]*)*[.!?]?['\"]?(?=\\s|$)",
            Pattern.MULTILINE | Pattern.COMMENTS);

    Matcher reMatcher = re.matcher(text);
    while (reMatcher.find()) {
        list.add(reMatcher.group());
    }
    String textDelimted[] = new String[list.size()];
    textDelimted = list.toArray(textDelimted);

    StringBuilder stringB = new StringBuilder(100);

    for (int i = 0; i < textDelimted.length; i++) {
        while (stringB.length() + textDelimted[i].length() < 250)
            stringB.append(textDelimted[i]);

        System.out.println("!#@#$%" +stringB.toString());
    }
}

预期结果:

[0]:也许年龄的影响远远暴露。现在她不信任你了,她真诚地表达了热烈的爱意。作为可以容忍的建议,他提出了无耻无情的反对意见。她虽然高兴地察觉到屏幕 throw 遇到了不吃的距离。

[1]:匆忙查看还是写了最亲爱的老人达天气吧。所以对女儿的指导是如此甜蜜或极端。提供现在拆封但带来。不愉快的惊讶减少了偏爱。吵闹和他们的意思。

[2]:死亡意味着民事上的献伤。恐怕直接叫方安了。分辨率减少信念所以先生在令人不快的简单没有。不,它作为早餐传达了认真直接的原则。

[3] 他的儿子性格幽默,克服了她单例汉的进步。学习却出于愿望却居住在幸运之窗。

最佳答案

我认为你需要稍微修改一下你的循环。 我的结果匹配。

import java.util.List;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class MyClass {
    public static void main(String args[]) {

        List<String> list = new ArrayList<String>();

        String text = "Perhaps far exposed age effects. Now distrusts you her delivered applauded affection out sincerity. As tolerably recommend shameless unfeeling he objection consisted. She although cheerful perceive screened throwing met not eat distance. Viewing hastily or written dearest elderly up weather it as. So direction so sweetness or extremity at daughters. Provided put unpacked now but bringing. Unpleasant astonished an diminution up partiality. Noisy an their of meant. Death means up civil do an offer wound of. Called square an in afraid direct. Resolution diminution conviction so mr at unpleasing simplicity no. No it as breakfast up conveying earnestly immediate principle. Him son disposed produced humoured overcame she bachelor improved. Studied however out wishing but inhabit fortune windows. ";

        Pattern re = Pattern.compile("[^.!?\\s][^.!?]*(?:[.!?](?!['\"]?\\s|$)[^.!?]*)*[.!?]?['\"]?(?=\\s|$)",
                Pattern.MULTILINE | Pattern.COMMENTS);

        Matcher reMatcher = re.matcher(text);
        while (reMatcher.find()) {
            list.add(reMatcher.group());
        }
        String textDelimted[] = new String[list.size()];
        textDelimted = list.toArray(textDelimted);

        StringBuilder stringB = new StringBuilder(300);

        for (int i = 0; i < textDelimted.length; i++) {
            if(stringB.length() + textDelimted[i].length() < 250) {
                stringB.append(textDelimted[i]);
            } else {
                System.out.println("!#@#$%" +stringB.toString());
                stringB = new StringBuilder(300);
                stringB.append(textDelimted[i]);
            }

        }
        System.out.println("!#@#$%" +stringB.toString());
    }
}

println 替换为以下代码以获取结果列表:

ArrayList<String> arrlist = new ArrayList<String>(5);
..
arrlist.add(stringB.toString());
..

关于java - 根据长度拆分并添加字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57525131/

相关文章:

ruby - 如何在 Ruby 中不使用反斜杠转义来执行字符串替换

python - 如何在Python中的嵌套列表中查找给定值的索引?

python - 2个列表的组合

java - Java 信号量默认使用忙等待还是等待/通知?

java - 实现接口(interface)时获得与父类(super class)相同的接口(interface)

java - 使用此运算符设置私有(private)变量与普通赋值有什么区别

python重新查找可能包含括号的字符串

java - 是否有 Apache Jena 的 C/C++ 绑定(bind)?

c - 在 C 中将指针与函数一起使用时出现错误

python - 如何快速迭代一个大列表?