URL encoding using java

Hi
When I encode a string value using URLEncoder.encode(strName) it replaces the white spaces in the string with "+" symbol.
However, want it to replace the white spaces to "%20" instead of "+" symbol so that it would be compatible across browsers and with JavaScript escape() and unescape() functions.

Is there a way to force URLEncoder to use "%20" instead of "+" signs?

Thanks in advance.

Regards,
ashishanandAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
hoomanvConnect With a Mentor Commented:
Original + signs will be converted to %2B
And spaces will become +
Then just call replaceAll("\\+", "%20");
and those + will become %20 too
0
 
hoomanvCommented:
You'll have to do it yourself by replacing all + signs with %20
0
 
hoomanvCommented:
str = str.replaceAll("\\+", "%20");
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ashishanandAuthor Commented:
I understand that it can be done by using replaceAll() however, I'm concerned with encoding / handling of special characters.

If we use replaceAll() , we would need to handle each of the special characters explicitly and this means more work (including testing task).

I'm looking for a generic solution.

Regards,
0
 
hoomanvCommented:
You are only concerned about one special char: space
You can correct it by including only one more line
0
 
ashishanandAuthor Commented:
Hi hoomany,

I'm not clear by what you have just said. Are you saying that I should be only concerned with just white space.

0
 
hoomanvCommented:
Talking clearly

> Is there a way to force URLEncoder to use "%20" instead of "+" signs?
Not that I'm aware of

However you can first url encode your url
Then replace all undesirable + signs with %20

> we would need to handle each of the special characters explicitly
You don't need, only white space as you are concerned about
0
 
ashishanandAuthor Commented:
As mentioned earlier, I'm not only concerned with white space. I'm also concerned about other special character.

Are saying that we should only be concerned about handling white space while working with java URLEncoder and javascript unescape.
0
 
hoomanvCommented:
> I'm also concerned about other special character
Could you name some of them ?
0
 
ashishanandAuthor Commented:
Some of the special characters are &*<>/?-_%'".\/|{}[]
0
 
hoomanvCommented:
And URLEncoder is responsible for all of them

URLEncoder.encode("&*<>/?-_%'.\\/|{}[]", "UTF-8"); --> %26*%3C%3E%2F%3F-_%25%27.%5C%2F%7C%7B%7D%5B%5D

So, You only need to replace + signes to %20
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.