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!!
7 Answers 7
You can do
StringBuilder sb = new StringBuilder();
String sep = "";
for (String s : listObjs) {
sb.append(sep).append(s);
sep = ", ";
}
System.out.println(sb);
-
Not sure if that's more or less efficient than the boolean check (probably counting nanoseconds here!)assylias– assylias2012年08月21日 11:18:33 +00:00Commented 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 = "[ ";
Peter Lawrey– Peter Lawrey2012年08月21日 11:22:44 +00:00Commented Aug 21, 2012 at 11:22
you may want to use Apache Commons StringUtils.join method.
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();
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());
-
you need to remove the trailing comma.assylias– assylias2012年08月21日 11:06:36 +00:00Commented Aug 21, 2012 at 11:06
-
Format is rather expensive. Do you really want a trailing ", "?Peter Lawrey– Peter Lawrey2012年08月21日 11:07:56 +00:00Commented Aug 21, 2012 at 11:07
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);
}
-
thnx for the info ...even i knew it theoretically..but practically you enlightened melowLatency– lowLatency2012年08月21日 11:17:44 +00:00Commented Aug 21, 2012 at 11:17
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();
}
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);
StringBuilder
(as mentioned many times) for this kind of string manipulations