Hello I have a url string like
http://example.com/foo/?bar=15&oof=myp
Now lets say that I want to change the int value in the bar parameter to 16, in order to have
http://example.com/foo/?bar=16&oof=myp
How can I do this? Considering that the number after the = might be of 1, 2 or ever 3 characters. Thank you
Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.
A URL which contains a page on your site should NEVER have a "/" after it (e.g. "foo. html/") A URL should always have a single question mark in it "?" URL Query String parameters should be separated by the ampersand "&"
You can use UriComponentsBuilder (it's part of Spring Web jar) like this:
String url = "http://example.com/foo/?bar=15&oof=myp";
UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromUriString(url);
urlBuilder.replaceQueryParam("bar", 107);
String result = urlBuilder.build().toUriString();
Substitute 107 with the number you want. With this method you can have URI or String object from urlBuilder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With