I want to join strings together, but when doing this it often happens that there is a comma too many, and therefore I need to remove that comma. In this code, I use the substring
to delete the two last characters.
How can this become more elegant?
List<String> paramList = new ArrayList<String>( );
paramList.add( "param1" );
paramList.add( "param2" );
StringBuilder result = new StringBuilder();
for ( String p : paramList )
{
result.append( p ).append( ", " );
}
String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) );
System.err.println( withoutLastComma );
result.append(", ").append(p)
--allows you to use the simpler extraction:result.substring(", ".length())
\$\endgroup\$result.deleteCharAt(result.length)
which doesn't fit in here) \$\endgroup\$