title: Elegant Solutions to the Fencepost Problem (with Strings)
body: <p>What I'm referring to is concatenating <code>String</code>s with a certain <code>String</code> in the middle, such as concatenating sentences separated by a period, or parameter lists with a comma. I know you can use libraries, but sometimes these can't do what you want, like when you want to generate the phrases you are concatenating. So far I've come up with two solutions,</p>
  
  <pre><code>StringBuffer sentence = new StringBuffer();
  String period = "";
  for ( int i = 0; i &lt; sentences.length; i++ ) {
      sentence.append( period + sentences[i] );
      period = ". ";
  }
  </code></pre>
  
  <p>which suffers from the redundant reassignment of <code>period</code>. There is also</p>
  
  <pre><code>StringBuffer actualParameters = new StringBuffer();
  actualParameters.append( parameters[0] );
  for ( int i = 1; i &lt; parameters.length; i++ ) {
      actualParameters.append( ", " + parameters[i] );
  }
  </code></pre>
  
  <p>which removes the reassignment but which still looks unappealing. Any other solutions are greatly appreciated.</p>