Two Laptop Bag
The moose likes Beginning Java and the fly likes replacing %20 in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "replacing %20 in a string" Watch "replacing %20 in a string" New topic

replacing %20 in a string

Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
Hi all,

can someone tell me as to how I can replace the "%20" in a string...
String.replaceAll("%20","");----> does not seem to work for this one


When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." <br />Linus Torvalds
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
It should work...

"%20".replaceAll("%20", "") --> "" for me...

Are you trying to decode URLs ?
Have a look at in this case.

Regards,<br />Seb<br /> <br />SCJP 1.4
Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
thats the code which is trying to replace

String projectPath="/playground/New%20Folder";

if (projectPath.indexOf("%20") > 0) {
projectPath.replaceAll("%20", "");
projectPath = projectPath.substring(1);
System.out.println("PROJECT PATH IS " + projectPath);

and the output I get is

PROJECT PATH IS playground/New%20Folder
Michael Lloyd Lee

Joined: Sep 07, 2005
Posts: 22
String is immutable. And so replaceAll does not effect the current string, but returns a new one.

Please please please use code tags!<br /> <br /><a href="" target="_blank" rel="nofollow">Java API</a> - <a href="" target="_blank" rel="nofollow">Java Tutorials</a>
Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
Thanx michael, it worked...and the next time I will be careful about posting the code using the prescribed format

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Why do you have an if statement? replaceAll() won't replace anything that isn't there. Of course, you may be worried about the cost of calling replaceAll() when there is nothing to replace. Does this happen very often? The current paradigm is to not worry about such optimizations until after you run code metrics to determine if it will make a significant difference in the run-time for your code.

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: replacing %20 in a string
It's not a secret anymore!