Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java strings concat

Tags:

java

string

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

like image 403
lowLatency Avatar asked Dec 04 '22 02:12

lowLatency


1 Answers

You can do

StringBuilder sb = new StringBuilder();
String sep = "";
for (String s : listObjs) {
    sb.append(sep).append(s);
    sep = ", ";
}
System.out.println(sb);
like image 193
Peter Lawrey Avatar answered Dec 25 '22 08:12

Peter Lawrey