Decode Java

Sometimes when working on web applications you need to encode GET parameter values that are part of a URL. Recently while working on a custom URL handler in Java I ran into a situation where I thought I had to encode part of the URL. I didn’t have to go that route, to encode/decode the custom URL, but this reminded me how to do that in Java. If you need to encode string value to be included in a URL use the URLEncoder as such:

String encoded = URLEncoder.encode("value: *(&#%");
// encoded would equal to "value%3A+*%28%26%23%25"

If you notice spaces will be converted to plus signs and characters like the pound sign (#) will be converted to their hexadecimal value. To decode a sting value you can use the URLDecoder’s decode(String) method.