Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URL Encode

 
ford Darcy Jr
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using URL encode in my servlet program to replace spaces. I need to change the + value with %20. Is there any way to do it since if I use URL encode its outputting "+" instead of "%20".

Thanks.
 
Ryan McGuire
Ranch Hand
Posts: 1057
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ford Darcy Jr:
Hi,

I am using URL encode in my servlet program to replace spaces. I need to change the + value with %20. Is there any way to do it since if I use URL encode its outputting "+" instead of "%20".

Thanks.

If you're using Java 1.4 or later, use String.replaceAll().

encoded = myString.replaceAll(" ", "%20");
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe something like URLEncoder would do the trick?
[ May 18, 2005: Message edited by: Pauline McNamara ]
 
Ryan McGuire
Ranch Hand
Posts: 1057
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pauline McNamara:
Maybe something like URLEncoder would do the trick?

[ May 18, 2005: Message edited by: Pauline McNamara ]


But that puts a '+' in place of a space. I think we're trying to get "%20" instead.

Ryan
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why? The + character is the proper encoding.
 
Ryan McGuire
Ranch Hand
Posts: 1057
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...depending on what he has in mind. If he's trying to generate URL's, then a URLEncoder probably really is the right thing to use. But then again, URLEncoder doesn't solve the problem explicitly stated in the original post.

However, it may just be that he wanted to change spaces to %20 for other reasons, saw the URLEncoder, tried it, and found that it was a bad fit for his application after all.

I was just assuming he knows what he wants to do even if he doesn't know how to do it and I don't know why he wants to. I also assume that if he tried URLEncoder and it changed spaces to +s, he would verify that +s are wrong for his purposes before posting the question.

Of course I could be wrong.

Ryan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic