tags:

views:

47

answers:

1

Hi

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?

+2  A: 

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.

Bozho