What's the best way to get rid of get parameters from url string?

Probably not the most efficient way, but more type safe :

private String getUrlWithoutParameters(String url) throws URISyntaxException {
    URI uri = new URI(url);
    return new URI(uri.getScheme(),
                   uri.getAuthority(),
                   uri.getPath(),
                   null, // Ignore the query part of the input url
                   uri.getFragment()).toString();
}

I normally use

url.split("\\?")[0]

Using javax.ws.rs.core.UriBuilder from JAX-RS 2.0:

UriBuilder.fromUri("https://www.google.co.nz/search?q=test").replaceQuery(null).build();

Using the very similar org.springframework.web.util.UriBuilder from Spring:

UriComponentsBuilder.fromUriString("https://www.google.co.nz/search?q=test").replaceQuery(null).build(Collections.emptyMap());

Tags:

Java