public static String replaceOrAddQueryParameter(String url, String key, String value) {
    String[] urlParts = url.split("\\?");
    String qParameter = key + "=" + value;

    if (urlParts.length == 2) {
      Map<String, List<String>> paramMap = extractParametersFromQueryString(urlParts[1]);
      if (paramMap.containsKey(key)) {
        String queryValue = paramMap.get(key).get(0);
        return url.replace(key + "=" + queryValue, qParameter);
      } else {
        return url + "&" + qParameter;
      }

    } else {
      return url + "?" + qParameter;
    }
  }
public static String replaceOrAddQueryParameter ( String url , String key , String value ) { String [ ] urlParts = url . split ( "\\?" ) ; String qParameter = key + "=" + value ; if ( urlParts . length == 2 ) { Map < String , List < String > > paramMap = extractParametersFromQueryString ( urlParts [ 1 ] ) ; if ( paramMap . containsKey ( key ) ) { String queryValue = paramMap . get ( key ) . get ( 0 ) ; return url . replace ( key + "=" + queryValue , qParameter ) ; } else { return url + "&" + qParameter ; } } else { return url + "?" + qParameter ; } }
Modify the query string in the given {@code url} and return the new url as String.
<p>
The given key/value pair is added to the url. If the key is already present, it is replaced with the new value.

@param url
The URL which parameters should be modified.
@param key
the key, that should be modified or added
@param value
the value of the key/value pair
@return the modified URL as String