java - Adding delimiter has any elegant way? -


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

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -