public static String join ( String separator , Collection < String > stringsToJoin ) { StringBuilder builder = new StringBuilder ( ) ; // Check if there is at least 1 element then use do/while to avoid trailing separator int index = stringsToJoin . size ( ) ; for ( String str : stringsToJoin ) { index -- ; if ( str != null ) { builder . append ( str ) ; if ( index > 0 ) { builder . append ( separator ) ; } } } return builder . toString ( ) ; }
Utility to join strings with a separator . Skips null strings and does not append a trailing separator .