Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String replaceAll method removes backslashes in replacement string

 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use String replaceAll() method, it removes all the backslashes in my replacement string. The matcher class in jdk explicity removes all the backslashes. Is there any way to avoid this?

when I do str.replaceAll("xyz", "c:\\temp\\xyz"); it replaces xyz with ctempxyz instead of c:\temp\xyz.

Does anyone know how to replace the string without replacing slashes? Any help is greatly appreciated. Thanks.
[ February 15, 2008: Message edited by: Raja Kannappan ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to use four backslashes:
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that before and it worked, but I had problems converting my input replacement string (coming from user) to the desired format. Previously, I tried replacement = replacement.replaceAll("\\", "\\\\"); but it gave me error. Now, i tried replacement = replacement.replaceAll("\\\\", "\\\\\\\\"); and it works fine.

The problem is solved but I don't understand why we have to put four slashes. I understand that we put two back slashes instead of one backslash as a escape
construct so that second backslash is actually taken as a character. But, I don't understand why we have to use four slashes. Can anyone please explain?

Thanks.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Raja]:But, I don't understand why we have to use four slashes. Can anyone please explain?


Obviously a forward slash is a special character in a String, but a forward slash is also a special character in a regex. The forward slash has to be escaped with another forward slash in order for the regex engine to view it as a literal '\' character. Both forward slashes have to be escaped so that the String class views them as literal '\'. Therefore you end up needing four forward slashed.
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Garrett. That helps!
[ February 17, 2008: Message edited by: Raja Kannappan ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
replace foreward by backward in Garretts text.

slash=forward slash=/=uphill
backslash=\=downhill

It's a latin alphabet, so you have to read from left to right.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Stefan]: replace foreward by backward in Garretts text.

slash=forward slash=/=uphill
backslash=\=downhill

It's a latin alphabet, so you have to read from left to right.


Thanks for the correction. I always get those confused. Now how do I remember the difference between a stalactite and a stalagmite.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:

Thanks for the correction. I always get those confused. Now how do I remember the difference between a stalactite and a stalagmite.

The m in stalagmite has 3 feet at the bottom, and the t in stalagtite has a hook at the top.
 
John O'Hanley
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an alternative to replaceAll that avoids regular expressions:

The String.replace(CharSequence, CharSequence) method

See as well :
javapractices.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic