【发布时间】:2019-08-16 12:59:58
【问题描述】:
我有一个段落作为输入字符串。我正在尝试将段落拆分为句子数组,其中每个元素包含的确切句子不超过 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] : 可能会暴露年龄效应。现在不信任你,她用真诚传递了鼓掌的感情。作为可以容忍的建议,他的反对意见是无耻的冷酷无情。她虽然开朗,但感觉屏蔽投掷却不吃距离。
[1] : 匆忙查看或写最亲爱的老人天气。所以方向如此甜蜜或极端的女儿。提供现在打开包装但带来。不愉快惊讶地减少了偏心。吵闹是他们的意思。
[2]:死亡意味着向上民事做一个提议伤口的。叫方安里怕直接。决议减少信念所以先生在不愉快的简单没有。不,它作为早餐传达了认真的直接原则。
[3] 儿子处理他的幽默感克服了她单身汉的进步。学习但希望但居住在幸运窗口。
【问题讨论】:
-
实际效果如何?您面临的问题是什么?你能提供一个可执行文件minimal reproducible example吗?
标签: java string list stringbuilder