I get a url string from a user and would like to transform it to a legal http url:
"http://one.two/three?four five" should turn into "http://one.two/three?four%20five"
however, URLEncoder does not help, as it encodes the entire string (including the legal "://").
help?
Use the URL
class. For example:
URL url = new URL(urlString);
String encodedQueryString = URLEncoder.encode(url.getQuery());
String encodedUrl = urlString.replace(url.getQuery(), encodedQueryString);
The third line might be different - for example constructing a new URL
from all its parts.
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