• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URL Encode

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe something like URLEncoder would do the trick?
[ May 18, 2005: Message edited by: Pauline McNamara ]
 
Ryan McGuire
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why? The + character is the proper encoding.
 
Ryan McGuire
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic