user11230 user11230 - 1 year ago 103
Java Question

How to substitute a query parameter in a string url

Hello I have a url string like

Now lets say that I want to change the int value in the bar parameter to 16, in order to have

How can I do this? Considering that the number after the = might be of 1, 2 or ever 3 characters. Thank you

Answer Source

You can use UriComponentsBuilder like this:

String url = "";

UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromUriString(url);

urlBuilder.replaceQueryParam("bar", 107);

String result =;

Substitute 107 with the number you want. With this method you can have URI or String object from urlBuilder.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download