public static String addQueryStringFragment ( String url , String fragment ) { StringBuilder newUrl = new StringBuilder ( url ) ; if ( fragment . startsWith ( "?" ) || fragment . startsWith ( "&" ) ) { fragment = fragment . substring ( 1 ) ; } if ( ! url . endsWith ( "?" ) && ! url . endsWith ( "&" ) ) { if ( url . contains ( "?" ) ) { newUrl . append ( ' ' ) ; } else { newUrl . append ( ' ' ) ; } } return newUrl . append ( fragment ) . toString ( ) ; }
Adds a query string fragment to the URL adding a ? if there s no query string yet .