• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String replaceAll method removes backslashes in replacement string

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to use four backslashes:
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Garrett. That helps!
[ February 17, 2008: Message edited by: Raja Kannappan ]
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic