【发布时间】:2014-11-10 20:26:29
【问题描述】:
我正在尝试编写代码以从段落中删除整个句子。不管是哪个句子,但至少要有一个。
String edit = "The cow goes moo. The cow goes boo. The cow goes roo. The cow goes jew.";
int sentencestart = (edit.substring(edit.length()/2).indexOf('.') + edit.length()/2);
int sentenceend = edit.substring(sentencestart).indexOf('.') + sentencestart;
edit = edit.substring(0, sentencestart) + edit.substring(sentenceend);
System.out.println(edit);
这是我目前拥有的代码。它目前正在打印与我开始时完全相同的字符串。有人有什么想法吗?
编辑:我错误地暗示应该删除任何句子。我的意思是除了第一句话之外的任何句子。最好将要删除的句子放在字符串中间的某个位置,而实际应用程序将用于非常大的字符串。
【问题讨论】:
-
删除每个字符,直到达到
. -
通过查看值来调试它以了解发生了什么,打印出 sentancestart 和 sentanceend 以查看它们是否不同。看起来他们正在评估相同的值。
标签: java string substring text-segmentation