public static String buildPath ( String first , String ... parts ) { StringBuilder result = new StringBuilder ( first ) ; for ( String part : parts ) { if ( result . length ( ) == 0 ) { if ( part . startsWith ( "/" ) ) { result . append ( part . substring ( 1 ) ) ; } else { result . append ( part ) ; } } else { if ( result . toString ( ) . endsWith ( "/" ) && part . startsWith ( "/" ) ) { result . append ( part . substring ( 1 ) ) ; } else if ( ! result . toString ( ) . endsWith ( "/" ) && ! part . startsWith ( "/" ) && ! part . isEmpty ( ) ) { result . append ( "/" ) . append ( part ) ; } else { result . append ( part ) ; } } } return result . toString ( ) ; }
Concatenate path parts into a full path taking care of extra or missing slashes .