【问题标题】:Breaking up paragraphs into String tokens将段落分解为字符串标记
【发布时间】:2014-10-14 04:48:55
【问题描述】:

我能够根据给定的第 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);

我正在使用任何第 n 个整数值设置 charLimit 变量。 mText 是带有一段文本的字符串。关于如何改进这一点的任何建议?提前谢谢你。

我收到了很好的回应,所以你知道我做了什么来弄清楚我是否登陆了一个空间,我使用了这个 while 循环。我只是不知道如何从这一点纠正。

while (!strTemp.substring(strTemp.length() - 1).equalsIgnoreCase(" ")) {
    // somehow refine string before added to array
}

【问题讨论】:

  • 您可以检查 mText 长度是否大于或等于 charlimit 与当前总单词长度之间的差异。
  • 检查每个字符是否为空白,每次为空白时,更新一个保存空白索引的变量。一旦你到达最后一次迭代,如果下一个字符不是空格,你就知道你在一个单词的中间。在这种情况下,您可以使用最后一个空白索引来了解您需要停止的位置。
  • @Jurgen,这也是我的想法。但是,这是我需要帮助的部分。让我告诉你我尝试了什么,但它不起作用。
  • 您可以使用 StringBuilder 并附加每个迭代的空白,以便附加每个单词,然后设置您的字符限制,并使用模数在每个循环中循环吗?

标签: java algorithm substring


【解决方案1】:

不确定我是否正确理解了您想要的内容,但可以回答我的解释:

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

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();

这将搜索charLimit(示例值)之前的最后一个空格,如果它小于tolerance,则将字符串拆分到那里,如果不是,则在charLimit 拆分。

此解决方案的唯一问题是最后一个 Stringtoken 可能比 charLimit 长,因此您可能需要调整 arrayLength 并循环 while (mText - j &gt; charLimit)


编辑

运行示例代码:

 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?

附加编辑: 根据 curiosu 的建议添加了 trim()。它会删除字符串标记周围的空格。

【讨论】:

  • 感谢您的回答,但是,这不起作用。事实上,现在已经没有文字了。话还是被打断了。
  • 单词在末尾(最后一个字符串)还是从中间?被截断的单词有多长?
  • 改进:为此解决方案添加 trim():-result[i] = mText.substring(j, splitpoint).trim(); -result[lastIndex] = mText.substring(j).trim();
  • 让我试试这个。看起来很有希望。
  • @curiosu 添加了您的建议
猜你喜欢
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多