user11230 user11230 - 4 months ago 27
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


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.