Modifying the port of a URI

No, that's pretty much it. 'Tis a bit verbose, granted, but it's not that complicated. :-)

If you're using Java EE rather than just the JDK, see Talha Ahmed Khan's answer, which uses Java EE's UriBuilder, which is still a one-liner but more elegant. That's not part of the JDK, but if you're doing a servlet or similar (or don't mind including the necessary jar)...


You can also use URIBuider

http://download.oracle.com/javaee/6/api/javax/ws/rs/core/UriBuilder.html

UriBuilder.fromURI(uri).port(port).build("foo", "bar");

Tags:

Java