Roland Turner

about | contact

Perl-style join() function in Java

This comes up pretty frequently and seems like an odd omission from the standard library, particularly given that String now has split(). Most attempts are a little untidy, here’s the simplest expression of it that I could come up with:

public static String join(String separator, Object... items)
  StringBuffer result = new StringBuffer();

  for (Object item : items)
    if (result.length() > 0)


  return result.toString();

There is a theoretical performance improvement to be had in not repeatedly calling length() but without an actual bottleneck situation to solve, I’d err on the side of simplicity.