4

I have got a list of string and now I want to take the list into a String with comma(,) separation is concat method the only way or there is some easy and good way

StringBuilder s = null;
List<String> listObjs (5 strings in it)
for(String listObj : listObjs)
s.append(listObj);
s.append(",");

EDIT: guys at SO rocks....slew of answers in seconds..wow!!

1
  • 1
    I think you should use StringBuilder (as mentioned many times) for this kind of string manipulations Commented Aug 21, 2012 at 11:04

7 Answers 7

12

You can do

StringBuilder sb = new StringBuilder();
String sep = "";
for (String s : listObjs) {
    sb.append(sep).append(s);
    sep = ", ";
}
System.out.println(sb);
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure if that's more or less efficient than the boolean check (probably counting nanoseconds here!)
@assylias its quite a bit shorter. ;) It is useful when the first "sep" is not empty e.g. String sep = "[ ";
5

you may want to use Apache Commons StringUtils.join method.

Comments

0

Indeed use a StringBuilder:

StringBuilder sb = new StringBuilder();
for (String string : listObjs) {
    sb.append(string);
    sb.append(", ");
}
// Remove the dangling ", "
int length = sb.length();
sb.delete(length - 2, length);

String combined = sb.toString();

Comments

0

Use a StringBuilder.

StringBuilder sb = new StringBuilder();
for (String obj : listObjs) {
    sb.append(String.format("%s,", obj));
    //or without format
    //sb.append(obj).append(",");
}
sb.deleteCharAt(sb.length()-1); // The last ","
System.out.println(sb.toString());

2 Comments

you need to remove the trailing comma.
Format is rather expensive. Do you really want a trailing ", "?
0

concat() does not modify your s reference (which is null by the way, so you should expect a NullPointerException for s.concat()). You should initialize s with "" and then append to it using s = s.concat(other) or s += other. Or better, use a StringBuilder which is far more efficient for this kind of usecase:

StringBuilder sb = new StringBuilder();
for (String obj : objList) {
    if (sb.length() == 0) {
        sb.append(',');
    }
    sb.append(obj);
}

1 Comment

thnx for the info ...even i knew it theoretically..but practically you enlightened me
0

Using concat is not a good idea as it will create a new String at each loop. You can use a StringBuilder instead which is more efficient from a memory perspective.

public static String getCsvFrom(java.util.Collection<?> objects) {
    if (objects == null || objects.isEmpty()) {
        return "";
    }
    StringBuilder s = new StringBuilder();
    boolean firstLoop = true;
    for (Object o : objects) {
        if (!firstLoop) {
            s.append(",");
        } else {
            firstLoop = false;
        }
        s.append(o);
    }
    return s.toString();
}

Comments

0

I wonder why you haven't tried this:

String str = "";
str += listObjs.get(0);
for (int i = 1; i < listObjs.size() - 1; i++ ) {
    str += "," + listObjs.get(i);
}
str += listObjs.get(listObjs.size() - 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.