Because Java is too much of a pain in the ass to have included one in the standard library, here's a simple string join function:
public static String join(String[] pieces){
return join(pieces, " ");
}
public static String join(String[] pieces, char sep){
return join(pieces, String.valueOf(sep));
}
public static String join(String[] pieces, String sep){
if(pieces.length == 0) return "";
StringBuffer buf = new StringBuffer();
buf.append(pieces[0]);
for(int i=1,n=pieces.length; i<n; i++)
buf.append(sep).append(pieces[i]);
return buf.toString();
}
Update: Of course, there's also a join function in org.apache.commons.lang.StringUtils.
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):