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.

Related posts:

  1. Java String Conversion Puzzlers
  2. Java Five-Oh #5: StringBuilder
  3. How To Print The Alphabet in Java?
  4. Generic Java
  5. Java 6 and JDBC 4.0

This entry was posted in Java, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*