9

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

user11230
  • 539
  • 1
  • 9
  • 19
  • 1
    have look into this : http://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection – Rockstar Oct 21 '16 at 13:15

2 Answers2

15

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.

amicoderozer
  • 2,046
  • 6
  • 28
  • 44
-3
  1. Use regex to find the number parameter in the string url
  2. Use String.replace() to replace the old parameter with the new parameter
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Max
  • 129
  • 8