简体   繁体   English

如何控制字符串迭代中定界符(,)的添加

[英]How to control adding of delimiter (,) in string iteration

I'm using this code that gets the values from the arraylist. 我正在使用此代码从arraylist获取值。 After a data has been retrieved, i want to concatenate a (,) after it. 检索到数据后,我想在其后连接一个(,)。 The concatenation of data will stop if retrieved data will make the length count of the whole string greater than 160. After which, i want to stop adding comma since no data will follow after. 如果检索到的数据会使整个字符串的长度计数大于160,则数据的连接将停止。此后,由于没有后续数据,我想停止添加逗号。 How can i do this? 我怎样才能做到这一点?

 List<Pending> pending = db.getAllPending();
 List<String> resultingStrings = new ArrayList<String>();
 String a = "";
 for (Pending pn : pending) {
     if (a.length() + pn.getPm_str().length() <= 160) {
         a+=pn.getPm_str();
         a+=",";
     } else {
         resultingStrings.add(a);
          a = pn.getPm_str();
     }
 }
 resultingStrings.add(a);

Output 输出量

RBSN/C1/12/3/4/8,CVTE/C1/2/3/4/1,RBSN/C1/4/2131/331/12,RBSN/C1/45/67/12/44,RBSN/C1/44/231/123/23213,RBSN/C1/444/55522/222/11,RBSN/C1/123/123/213123/1231232, RBSN / C1 / 12/3/4/8,CVTE / C1 / 2/3/4/1,RBSN / C1 / 4/2131/331/12,RBSN / C1 / 45/67/12/44,RBSN / C1 / 44/231/123/23213,RBSN / C1 / 444/55522/222/11,RBSN / C1 / 123/123/213123/1231232,

There's a comma after the last concatenated data. 最后连接的数据后面有一个逗号。 Total string length: 146 字符串总长: 146

Expected Output 预期产量

RBSN/C1/12/3/4/8,CVTE/C1/2/3/4/1,RBSN/C1/4/2131/331/12,RBSN/C1/45/67/12/44,RBSN/C1/44/231/123/23213,RBSN/C1/444/55522/222/11,RBSN/C1/123/123/213123/1231232 RBSN / C1 / 12/3/4/8,CVTE / C1 / 2/3/4/1,RBSN / C1 / 4/2131/331/12,RBSN / C1 / 45/67/12/44,RBSN / C1 / 44/231/123/23213,RBSN / C1 / 444/55522/222/11,RBSN / C1 / 123/123/213123/1231232

No comma after the last data concatenated. 最后一个数据串联后没有逗号。 Total string length: 145 字符串总长: 145

Rather than adding the comma after you add the element add it before so you'll never have a comma at the end. 而不是在添加元素之后添加逗号,而是在元素之前添加逗号,这样您就永远不会在末尾使用逗号了。

List<Pending> pending = db.getAllPending();
List<String> resultingStrings = new ArrayList<String>();
String a = "";
for (Pending pn : pending) {
  if (a.length() + pn.getPm_str().length() < 160) { // < to leave room for the comma as well
        if(a.length() != 0) { // don't add a comma before the first 1
          a+=",";
        }
        a+=pn.getPm_str();
  } else {
        resultingStrings.add(a);
        a = pn.getPm_str();
  }
}
resultingStrings.add(a);

Add one line to the else statement: 在else语句中添加一行:

List<Pending> pending = db.getAllPending();
List<String> resultingStrings = new ArrayList<String>();
String a = "";

for (Pending pn : pending) {
    if (a.length() + pn.getPm_str().length() <= 160) {
        a+=pn.getPm_str();
        a+=",";
    }
    else {
        a = a.substring(0, a.length()-1)  <-----
        resultingStrings.add(a);
        a = pn.getPm_str();
    }
}
resultingStrings.add(a);

This is called the trailing comma problem. 这称为尾随逗号问题。 There are a lot of ways to solve this. 有很多方法可以解决此问题。

One way is to take the final string and just remove the last character: 一种方法是获取最后一个字符串,然后删除最后一个字符:

s = s.substring(0, s.length()-1);

Another way is to convert your loop to a regular for loop and check whether it's not at the end before you add a comma. 另一种方法是将循环转换为常规的for循环,并在添加逗号之前检查循环是否未结束。

 List<Pending> pending = db.getAllPending();
 List<String> resultingStrings = new ArrayList<String>();
 String a = "";
 for (int i = 0; i < pending.size(); i++) {
     Pending p = pending.get(i);
     if (a.length() + pn.getPm_str().length() <= 160) {
         a+=pn.getPm_str();
         if(i != pending.size()-1){
             a+=",";
         }
     } else {
         resultingStrings.add(a);
          a = pn.getPm_str();
     }
 }
 resultingStrings.add(a);

Use StringBuilder. 使用StringBuilder。 Once the string is ready, delete the last character, cast it to String and you have your answer. 字符串准备好后,删除最后一个字符,将其转换为String即可得到答案。

List<Pending> pending = db.getAllPending();
 List<String> resultingStrings = new ArrayList<String>();
 StringBuilder a = new StringBuilder;
 for (Pending pn : pending) 
     if (a.length() + pn.getPm_str().length() <= 160) {
         a.append(pn.getPm_str()).append(",");
     }
     else {
         a.deleteCharAt(a.length());
         resultingStrings.add(a);
         a = new StringBuilder();
         a.append(pn.getPm_str());
     }
 a.deleteCharAt(a.length());
 resultingStrings.add(a.toString());

Use StringBuilder (for efficiency), and only insert a comma when necessary. 使用StringBuilder(为了提高效率),仅在必要时插入逗号。

List<Pending> pending = db.getAllPending();
List<String> resultingStrings = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (Pending pn : pending) {
    final String str = pn.getPm_str();
    if (sb.length() > 0) {
        sb.append(',');
    }
    if (sb.length() + str.length() <= 160) {
        sb.append(str);
    } else {
        resultingStrings.add(sb.toString());
        sb.setLength(0);
        sb.append(str);
    }
}
resultingStrings.add(sb.toString());

Why do not you add directly to resultingString List? 为什么不直接将其添加到resultingString列表? Since List stores the strings separated by comma you do not need to do it by yourself. 由于列表存储用逗号分隔的字符串,因此您无需自己执行。 And you can count the length of the strings stored using resultingString.toString().length() . 您可以计算使用resultingString.toString().length()存储的字符串的resultingString.toString().length()

List<Pending> pending = db.getAllPending();
List<String> resultingStrings = new ArrayList<String>();
for (Pending pn : pending) {
     // 162 considering the starting and closing brackets
     if ((resultingStrings.toString().length()+pn.getPm_str())<= 162) {
         resultingStrings.add(pn.getPm_str());
      }
  }

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

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