我想在特定长度后拆分字符串而不切掉任何单词,不等于String

时间:2019-09-20 11:48:57

标签: java string java-8 javafx-8

当长度为35时,我想分割一个字符串,例如“仅卢比2 148和68”,我想将此字符串分割为2。我尝试使用此代码对字符串进行分割。

String text = "Rupees Two Hundred Forty One and Sixty Eight only";
List<String> parts = new ArrayList<>();
int length = text.length();
for (int i = 0; i < length; i += 35) {
    parts.add(text.substring(i, Math.min(length, i + size)));

但是输出是这样的。

[Rupees Two Hundred Forty One and Si, xty Eight only]

但是我想要这样分割字符串。

[Rupees Two Hundred Forty One and, Sixty Eight only]

分割字符串时没有剪切词。 字符串根据帐单金额每次都不同。

5 个答案:

答案 0 :(得分:2)

您可能无法完全做到这一点。但是,使用String.indexOf()查找从35开始的第一个空格。然后使用substring方法对字符串进行分割。

      String text = "Rupees Two Hundred Forty One and Sixty Eight only";
      int i = text.indexOf(" ", 35);
      if (i < 0) {
         i = text.length();
      }
      String part1 = text.substring(0,i).trim();
      String part2 = text.substring(i).trim();

这是另一种方法。尚未完全检查边境情况。

      String[] words = text.split(" ");
      int k;
      part1 = words[0];
      for (k = 1; k < words.length; k++) {
         if (part1.length() >= 35 - words[k].length()) {
            break;
         }
         part1 += " " + words[k];
      }
      if (k < words.length) {
         part2 = words[k++];
         while (k < words.length) {
            part2 += " " + words[k++];
         }
      }
      System.out.println(part1);
      System.out.println(part2);

答案 1 :(得分:1)

只需在i+35位置搜索首选位置。要考虑的一件事是,当没有这样的位置时,即单词超出指定大小时,应该发生什么。以下代码将强制执行大小限制,如果找不到合适的位置,则中断单词的中间:

List<String> parts = new ArrayList<>();
int size = 35, length = text.length();
for(int i = 0, end, goodPos; i < length; i = end) {
    end = Math.min(length, i + size);
    goodPos = text.lastIndexOf(' ', end);
    if(goodPos <= i) goodPos = end; else end = goodPos + 1;
    parts.add(text.substring(i, goodPos));
}

如果中断发生在空格字符处,则空格将从结果字符串中删除。

答案 2 :(得分:0)

您可以找到索引“ and”,并将字符串从0子串到“ and”的索引。

 int i = text.indexOf("and") + 3;
 String part1 = text.substring(0,i);
 String part2 = text.substring(i).trim();

答案 3 :(得分:0)

我将使用StringBuilders从头开始构建String。一个带有一些注释的示例:

    String text = "Rupees Two Hundred Forty One and Sixty Eight only For seven thousand chickens";
    String split[] = text.split(" "); // Split by space
    // One SB for each sentence
    StringBuilder sentence = new StringBuilder();
    // One SB for the total String
    StringBuilder total = new StringBuilder();
    for (int i = 0; i < split.length; i++) {
        String word = split[i];
        // Check if that words fits to sentence
        if (sentence.length() + word.length() <= 35) {
            sentence.append(word);
            sentence.append(" ");
        } else {
            total.append(sentence.toString().trim());
            total.append(", ");
            // Flush sentence to total and start next sentence
            sentence = new StringBuilder();
            sentence.append(word);
            sentence.append(" ");
        }
    }
    //Add any leftover
    if (sentence.length() > 0)
        total.append(sentence.toString().trim());
    System.out.println(total.toString());

哪个输出到:

  

卢比241和688,仅七千只鸡

答案 4 :(得分:0)

我认为您可以使用while循环来计算包含最后一个空格字符的单词:

public static List<String> split(String str, int length) {
    List<String> res = new ArrayList<>();
    int prvSpace = 0;
    int from = 0;

    while (prvSpace < str.length()) {
        int pos = str.indexOf(' ', prvSpace + 1);

        if (pos == -1) {
            res.add(str.substring(from));
            prvSpace = str.length();
        } else if (pos - from < length)
            prvSpace = pos;
        else {
            res.add(str.substring(from, prvSpace));
            from = prvSpace + 1;
        }
    }

    return res;
}

演示:

in: "RupeesTwoHundredFortyOneandSixtyEightonly"
out: ["RupeesTwoHundredFortyOneandSixtyEightonly"]

in: "Rupees Two Hundred Forty One and Sixty Eight only"
out: ["Rupees Two Hundred Forty One and", "Sixty Eight only"]