• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replacing String with BackSlash

 
Vasudevan Gopalan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried to replace the single Codes with BackSlash and singleQuotes but it is not replacing

I tried giving two backslashes also but still it is not replacing.

Regards
Vasu
 
C. Nimo
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
This is a bit tricky, but I got you an answer.
What happens is - because you are using a String object,
which is immutable, the JVM has to concatenate it in order to
come up with the final answer.
So, suppose you have a String that looks like that: "\\\'"
which translates to "\'" -
when concatenating it inside the targer string, you
get only a "\'" - and that translates back into a single quote.
To get around it, you actually need to put in five backslashes.
so - you'd put
System.out.println(s.replaceAll("'","\\\\\'");
the second string translates into "\\'".
and then - upon the concatenation inside the result string
you get your desired "\'".
Have Fun (always)
Nimo.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic