java - Adding delimiter has any elegant way? -
this question has answer here:
- java: convert list<string> string 20 answers
- java equivalents of c# string.format() , string.join() 16 answers
this simple java coding question. have list of string
[say "hello" "how" "are" "you?"
]. need insert delimiter [-
] between each element of list output hello-how-are-you?
one simple way of doing below:
private static string adddelim(list<string> a) { string s = ""; for(int i=0; i<a.size(); i++) { if(i != 0) // don't add if first element { s += "-"; } s += a.get(i); } return s; }
is there elegant way of doing this?
from apache commons lang:
string out = stringutils.join(yourlist, '-');
Comments
Post a Comment