views:

109

answers:

3

I want to use java's URLEncoder class to encode my url strings. However, by default it converts spaces to '+' (plus sign). I'd like it to use '-' instead (similar to how this site encodes its urls).

I'm wondering what the best way to do this is? Should I just use its built in functionality and then do a string replace (from '+' to '-')? Thanks

+5  A: 

The URLEncoder encodes the URL's according a specific contract. You can and should not change it. Indeed just do a string replace afterwards, or maybe better, beforehand. The hyphen - is namely already a valid character in URL.

String encodedURL = URLEncoder.encode(url.replace(" ", "-"), "UTF-8");
BalusC
+1  A: 

Agreed with @BalusC.

I would also like to point out that what you are trying to do is not really "encoding" in the normal sense of the phrase "URL encoding". Encoding implies that that there is a reverse decoding step that will give you the original URL back. Unless you have some business rule that precludes "-" characters (e.g. hyphens if you are creating URL names from "text"), your transformation will not be reversible.

What you appear to be doing is transforming URL strings into other URL strings according to some rule that probably not reversible. It is not at all surprising that the URLEncoder class (which implements a particular standardised reversible encoding) is not implementing your (application specific) transformation.

As @BalusC points out, the correct approach is to transform the URL strings (according to your application's needs) before you encode them. Indeed, it may be better (e.g. safer) to implement the transformation on the URL string's components ... before you assemble the complete URL.

(And I am taking for granted that the URLs are being used in a context that actually require the encoding implemented by URLEncoder.)

Stephen C
+1  A: 

This site doesn't 'encode its URLs' with hyphens, it defines them that way in the first place, apparently so that no encoding is necessary. That's up to how they generate their URLs. You can do the same, but that's not URLEncoding, that's just generating URLs. Using URLEncoder for this task, which it is not designed for, and complaining that it does exactly what it is supposed to do and not what you want is pretty pointless. As BalusC said, URLEncoder is part of a contract between your browser and the Web servers of the world. You can't change that.

EJP