What I'm referring to is concatenating String
s with a certain String
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,
StringBuffer sentence = new StringBuffer();
String period = "";
for ( int i = 0; i < sentences.length; i++ ) {
sentence.append( period + sentences[i] );
period = ". ";
}
which suffers from the redundant reassignment of period
. There is also
StringBuffer actualParameters = new StringBuffer();
actualParameters.append( parameters[0] );
for ( int i = 1; i < parameters.length; i++ ) {
actualParameters.append( ", " + parameters[i] );
}
which removes the reassignment but which still looks unappealing. Any other solutions are greatly appreciated.