• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replacing %20 in a string

 
Vijaishanker bala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should work...

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

Are you trying to decode URLs ?
Have a look at java.net.URLDecoder in this case.
 
Vijaishanker bala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String is immutable. And so replaceAll does not effect the current string, but returns a new one.
Try
 
Vijaishanker bala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic