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!!

asked Aug 21, 2012 at 11:03
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);
answered Aug 21, 2012 at 11:08
2
  • Not sure if that's more or less efficient than the boolean check (probably counting nanoseconds here!) Commented Aug 21, 2012 at 11:18
  • 4
    @assylias its quite a bit shorter. ;) It is useful when the first "sep" is not empty e.g. String sep = "[ "; Commented Aug 21, 2012 at 11:22
5
assylias
330k83 gold badges680 silver badges806 bronze badges
answered Aug 21, 2012 at 11:11
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();
answered Aug 21, 2012 at 11:10
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());
answered Aug 21, 2012 at 11:05
2
  • you need to remove the trailing comma. Commented Aug 21, 2012 at 11:06
  • Format is rather expensive. Do you really want a trailing ", "? Commented Aug 21, 2012 at 11:07
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);
}
answered Aug 21, 2012 at 11:11
1
  • thnx for the info ...even i knew it theoretically..but practically you enlightened me Commented Aug 21, 2012 at 11:17
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();
}
answered Aug 21, 2012 at 11:12
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);
answered Aug 21, 2012 at 11:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.