This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Replacing String with BackSlash Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replacing String with BackSlash" Watch "Replacing String with BackSlash" New topic

Replacing String with BackSlash

Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
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.

C. Nimo
Ranch Hand

Joined: Mar 23, 2004
Posts: 82
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
the second string translates into "\\'".
and then - upon the concatenation inside the result string
you get your desired "\'".
Have Fun (always)
I agree. Here's the link:
subject: Replacing String with BackSlash
It's not a secret anymore!