简体   繁体   English

java - 按长度拆分字符串,保持子字符串完整

[英]java - split string by length, keeping substrings whole

I've split the string "this is a test for thestring longer than" into substrings of length 7 (or less), so i have a List strList that looks like: [this, is, a, test, for, thestri, ng, longer, than]我已经将字符串“this is a test for thestring long than”拆分为长度为 7(或更短)的子字符串,所以我有一个 List strList,它看起来像:[this, is, a, test, for, thestri, ng , 长于]

i need to print this out in rows of length of 7, keeping substrings whole: [this is, a test, for, thestri, ng, longer, than]我需要在长度为 7 的行中打印出来,保持子字符串完整:[这是一个测试,对于,thestri,ng,比]

but im getting this instead and i cant find the bug [this, this is, a test, for for, thestri, longer, ng than, seven, charact, ers]但我得到了这个,我找不到错误 [这个,这是,一个测试,for,thestri,long,ng than,七,charact,ers]

so what i'm trying to do is get the length of the substring and compare it to the limit of 7 characters (L1) and if it less than L1 - take next substring and add it to the same row (providing sum of their lengths + whitespace between them is still 7 or less).所以我想要做的是获取 substring 的长度并将其与 7 个字符(L1)的限制进行比较,如果它小于 L1 - 取下一个 substring 并将其添加到同一行(提供它们的长度之和+ 它们之间的空白仍然是 7 或更少)。 new strings then added to List strList2然后将新字符串添加到列表 strList2

package mylessons;
import java.util.*;

public class Level160 {

    public static void main(String[] args) {
         int L1 = 7; // length limit
         int len1 = 0;
         String tmpStr1 = "";
         List<String> strList = new ArrayList<>();
         List<String> strList2 = new ArrayList<>();

         strList.add("this");
         strList.add("is");
         strList.add("a");
         strList.add("test");
         strList.add("for");
         strList.add("thestri");
         strList.add("ng");
         strList.add("longer");
         strList.add("than");
         System.out.println(strList);

         for (int k = 0; k < strList.size(); k++ ) {

                // substring is shorter than L1 - 1 -->> adding to tmpStr1
                if (strList.get(k).length() + len1 < L1 - 1) {
                    tmpStr1 = tmpStr1 + strList.get(k).toString() + " ";
                    len1 = (len1 + strList.get(k).length() + 1);
                }

                // substring is L - 1 (whitespace is last char) -->> adding to tmpStr -->> adding to strList2
                if (strList.get(k).length() + len1 == L1 - 1) {
                    tmpStr1 = tmpStr1 + strList.get(k).toString();
                    strList2.add(tmpStr1);
                    len1 = 0;
                    tmpStr1 = "";
                }

                // substring is L -->> adding to tmpStr -->> adding to strList2
                if (strList.get(k).length() + len1 == L1) {
                    tmpStr1 = tmpStr1 + strList.get(k).toString();
                    strList2.add(tmpStr1);
                    len1 = 0;
                    tmpStr1 = "";
                }

                if (strList.get(k).length() + len1 > L1) {
                    strList2.add(strList.get(k));
                }
            }
            System.out.println(strList2);
    }
}

any help appreciated:)任何帮助表示赞赏:)

In the first iteration you run into the first if clause and into the second.在第一次迭代中,您遇到第一个 if 子句和第二个。 But the second if clause doesn't clear tmpStr1.但是第二个 if 子句不清除 tmpStr1。 That might not be the only problem with your code but it hints to two things you should try to do when fixing your code in my opinion:这可能不是您的代码的唯一问题,但它暗示了您在修复代码时应该尝试做的两件事:

  • Make sure you run into exactly one if clause every iteration.确保每次迭代都恰好遇到一个 if 子句。
  • Clear tmpStr1 and len1 every time you add something to strList2.每次向 strList2 添加内容时清除 tmpStr1 和 len1。

ok, so i've made some adjustments, so it's a working piece of code you can use:) also i've added check for last substring in given string being shorter than given length limit.好的,所以我做了一些调整,所以它是你可以使用的工作代码:) 我还添加了检查给定字符串中最后一个 substring 是否短于给定长度限制。

List before: [this, is, a, test, for, thestri, ng, longer, than] List after: [this is, a test, for, thestri, ng, longer, than]列出之前:[this, is, a, test, for, thestri, ng, long, than] 列出之后:[this is, a test, for, thestri, ng, long, than]

public static void main(String[] args) {
     int L1 = 7; // length limit
     int len1 = 0;
     String tmpStr1 = "";
     List<String> strList = new ArrayList<>();
     List<String> strList2 = new ArrayList<>();

     strList.add("this");
     strList.add("is");
     strList.add("a");
     strList.add("test");
     strList.add("for");
     strList.add("thestri");
     strList.add("ng");
     strList.add("longer");
     strList.add("than");
     System.out.println(strList);

     for (int k = 0; k < strList.size(); k++ ) {

            // last substring is shorter than L1
            if (strList.get(k).length() + len1 < L1 - 1 && k == strList.size() - 1) {
                strList2.add(strList.get(k));
                continue;
            }

            // substring is shorter than L1 -->> adding to tmpStr1
            if (strList.get(k).length() + len1 < L1 - 1) {
                tmpStr1 = tmpStr1 + strList.get(k).toString() + " ";
                len1 = (len1 + strList.get(k).length() + 1);
                continue;
            }

            // substring is L - 1 (whitespace is last char) -->> adding to tmpStr -->> adding to strList2
            if (strList.get(k).length() + len1 == L1 - 1) {
                tmpStr1 = tmpStr1 + strList.get(k).toString();
                strList2.add(tmpStr1.trim());
                len1 = 0;
                tmpStr1 = "";
            }

            // substring is L -->> adding to tmpStr -->> adding to strList2
            if (strList.get(k).length() + len1 == L1) {
                tmpStr1 = tmpStr1 + strList.get(k).toString();
                strList2.add(tmpStr1.trim());
                len1 = 0;
                tmpStr1 = "";
            }

            if (strList.get(k).length() + len1 > L1) {
                strList2.add(tmpStr1.trim());
                strList2.add(strList.get(k));
                len1 = 0;
                tmpStr1 = "";
            }

        }
        System.out.println(strList2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM