This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "replacing %20 in a string" Watch "replacing %20 in a string" New topic
Author

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
thanx

vijai


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 java.net.URLDecoder 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) {
System.out.println("found");
projectPath.replaceAll("%20", "");
}
projectPath = projectPath.substring(1);
System.out.println("PROJECT PATH IS " + projectPath);


and the output I get is

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

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


Please please please use code tags!<br /> <br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/" target="_blank" rel="nofollow">Java API</a> - <a href="http://java.sun.com/docs/books/tutorial/index.html" 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

vijai
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: replacing %20 in a string
 
Similar Threads
Dynamic Binding
StringTokenizer
two dimensional array
Array Declaration
DatagramPacket to String