Ha! Thanks to this post I've found another way to do this:
public static class Extensions
{
public static string JoinWith(this IEnumerable<string> strings, string separator)
{
return String.Join(separator, strings.ToArray());
}
}
Of course this is in C# now and Java won't (yet) support the extension method, but you ought to be able to adapt it as needed — the main thing is the use of String.Join
anyway, and I'm sure java has some analog for that.
Also note that this means doing an extra iteration of the strings, because you must first create the array and then iterate over that to build your string. Also, you will create the array, where with some other methods you might be able to get by with an IEnumerable that only holds one string in memory at a time. But I really like the extra clarity.
Of course, given the Extension method capability you could just abstract any of the other code into an extension method as well.