Java 将段落分解为字符串标记

Java 将段落分解为字符串标记,java,algorithm,substring,Java,Algorithm,Substring,我能够根据给定的第n个字符限制将文本段落分解为子字符串。我遇到的冲突是,我的算法正是这样做的,并且正在分解单词。这就是我被困的地方。如果字符限制发生在单词的中间,那么如何返回到一个空间,以便所有的子字符串都有整字? 这就是我正在使用的算法 int arrayLength = 0; arrayLength = (int) Math.ceil(((mText.length() / (double) charLimit))); String[] result = new String[arrayLe

我能够根据给定的第n个字符限制将文本段落分解为子字符串。我遇到的冲突是,我的算法正是这样做的,并且正在分解单词。这就是我被困的地方。如果字符限制发生在单词的中间,那么如何返回到一个空间,以便所有的子字符串都有整字?

这就是我正在使用的算法

int arrayLength = 0;
arrayLength = (int) Math.ceil(((mText.length() / (double) charLimit)));

String[] result = new String[arrayLength];
int j = 0;
int lastIndex = result.length - 1;
for (int i = 0; i < lastIndex; i++) {
    result[i] = mText.substring(j, j + charLimit);
    j += charLimit;
}

result[lastIndex] = mText.substring(j);

我不确定我是否正确理解了你想要的,但我的解释有一个答案:

您可以使用找到字符限制之前的最后一个空格,然后检查是否足够接近您的限制(对于没有空格的文本),即:

输出:

I am able to break up paragraphs of text
 into substrings based upon nth given
 character limit. The conflict I have is
 that my algorithm is doing exactly
 this, and is breaking up words. This is
 where I am stuck. If the character
 limit occurs in the middle of a word,
 how can I back track to a space so that
 all my substrings have entire words?

附加编辑:根据库里奥苏的建议添加了修剪()。它会删除字符串标记周围的空白。

您可以检查多行文字长度是否大于或等于charlimit和当前总字数长度之间的差值。如果每个字符都是空白,请检查其是否为空白,每次都更新包含空白索引的变量。一旦你进入了最后一次迭代,如果下一个字符不是空白,你就知道你在一个单词的中间。在这种情况下,您可以使用最后一个空白索引来知道您需要停止的位置。@Jurgen,这也是我所想的。然而,这是我需要帮助的部分。让我向您展示我尝试的内容,但它不起作用。您是否可以使用StringBuilder并追加每次迭代的空白,以便追加每个单词,然后设置字符限制,并使用模数在for each循环中循环?谢谢您的回答,但是,这不起作用。事实上,现在已经没有文字了。但是这些词还是被剪掉了。是从末尾(最后一个字符串)还是从中间?被截断的单词有多长?改进:为这个解决方案添加trim():-result[i]=mText.substring(j,splitpoint.trim()-结果[lastIndex]=mText.substring(j.trim();让我试试这个。“看起来很有希望。”库里奥苏补充了你的建议
int arrayLength = 0;
arrayLength = (int) Math.ceil(((mText.length() / (double) charLimit)));

String[] result = new String[arrayLength];
int j = 0;
int tolerance = 10;
int splitpoint;
int lastIndex = result.length - 1;
for (int i = 0; i < lastIndex; i++) {
    splitpoint = mText.lastIndexOf(' ' ,j+charLimit);
    splitpoint = splitpoint > j+charLimit-tolerance ? splitpoint:j+charLimit;
    result[i] = mText.substring(j, splitpoint).trim();
    j = splitpoint;
}

result[lastIndex] = mText.substring(j).trim();
 public static void main(String[] args) {
    String mText =  "I am able to break up paragraphs of text into substrings based upon nth given character limit. The conflict I have is that my algorithm is doing exactly this, and is breaking up words. This is where I am stuck. If the character limit occurs in the middle of a word, how can I back track to a space so that all my substrings have entire words?";

    int charLimit = 40;
    int arrayLength = 0;
    arrayLength = (int) Math.ceil(((mText.length() / (double) charLimit)));

    String[] result = new String[arrayLength];
    int j = 0;
    int tolerance = 10;
    int splitpoint;
    int lastIndex = result.length - 1;
    for (int i = 0; i < lastIndex; i++) {
        splitpoint = mText.lastIndexOf(' ' ,j+charLimit);
        splitpoint = splitpoint > j+charLimit-tolerance ? splitpoint:j+charLimit;
        result[i] = mText.substring(j, splitpoint);
        j = splitpoint;
    }

    result[lastIndex] = mText.substring(j);

    for (int i = 0; i<arrayLength; i++) {
        System.out.println(result[i]);
    }
}
I am able to break up paragraphs of text
 into substrings based upon nth given
 character limit. The conflict I have is
 that my algorithm is doing exactly
 this, and is breaking up words. This is
 where I am stuck. If the character
 limit occurs in the middle of a word,
 how can I back track to a space so that
 all my substrings have entire words?