有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我想在特定长度后拆分一个字符串,而不剪切任何单词,而不是相等的字符串

我想把一根绳子分开,比如“贰佰肆拾壹元陆角捌卢比整”,当它的长度是35时,我想把这根绳子分成两半。我尝试了这个代码来分割字符串

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) 个答案

  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. # 2 楼答案

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

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

    我将使用StringBuilders从头开始构建字符串。下面是一个例子,里面有一些评论:

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

    哪些输出到:

    Rupees Two Hundred Forty One and, Sixty Eight only For seven thousand, chickens

  4. # 4 楼答案

    我认为可以使用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"]
    
  5. # 5 楼答案

    你可能无法完全做到这一点。但是使用字符串。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);