Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

replaceAll()  RSS feed

 
Kristian Ledung
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I am wondering how to use the replaceAll() method of the String class.
I want to replace all & with & like this:


There is something about regex but I don�t know how to use it.
 
Kristian Ledung
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It�s supposed to be:
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm.... I don't think the "&" have any special meaning in a regex. So the replaceAll() should have worked.

What is happening when you do that replaceAll()?

Henry
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sould work fine just as you have it written.

[ May 04, 2006: Message edited by: Garrett Rowe ]
 
Kristian Ledung
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your fast answer!

Your example worked. But my code:



Doesn�t work. I don�t know why, but now I know that regex has nothing to do with it.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable. It should be...



Henry
 
Kristian Ledung
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm.... I missed one important thing.
I misssed this

url = url.replaceAll("&","& amp;");

Now it works!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!