Java and RFC 3986 URI encoding

Mark picture Mark · May 3, 2011 · Viewed 12.9k times · Source

is there a class to encode a generic String following the RFC 3986 specification?

That is: "hello world" => "hello%20world" Not (RFC 1738): "hello+world"

Thanks

Answer

MeBigFatGuy picture MeBigFatGuy · May 3, 2011

If it's a url, use URI

URI uri = new URI("http", "//hello world", null);
String urlString = uri.toASCIIString();
System.out.println(urlString);