public static String appendPiecesForUri ( String ... pieces ) { if ( pieces == null || pieces . length == 0 ) return "" ; // join parts && strip double slashes StringBuilder builder = new StringBuilder ( 16 * pieces . length ) ; char previous = 0 ; for ( int i = 0 ; i < pieces . length ; i ++ ) { String piece = pieces [ i ] ; if ( piece != null && piece . length ( ) > 0 ) { for ( int j = 0 , maxlen = piece . length ( ) ; j < maxlen ; j ++ ) { char current = piece . charAt ( j ) ; if ( ! ( previous == ' ' && current == ' ' ) ) { builder . append ( current ) ; previous = current ; } } if ( i + 1 < pieces . length && previous != ' ' ) { builder . append ( ' ' ) ; previous = ' ' ; } } } return builder . toString ( ) ; }
Takes any number of Strings and appends them into a uri making sure that a forward slash is inserted between each piece and making sure that no duplicate slashes are in the uri