• 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

Char Conversion

 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
how do you replace a simple quote with a backslash followed by a simple quote ?

That is I want to replace ' with \'





Thanks
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
\ is a special character in Java Strings so you need to escape it.
 
Celinio Fernandes
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually need 4 backslahes !



Thanks anyway.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Celinio Fernandes:
I actually need 4 backslahes !

Thanks anyway.



The reason that you need four backslashes is because the backslash has special meaning in a regex replacement string. And as mentioned, the backslash has special mean in a Java string literal.

So, to have one literal backslash, you need to escape the backslash with a backslash to turn off the regex replacement string meaning. Then you must escape the two backslashes, each with a backslash to turn off the special meaning in the Java string literal..... hance, 4 backslashes.

Henry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
myString.replace('\'', '\\');
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thavanathan Thangaraj:
myString.replace('\'', '\\');

Welcome to JavaRanch

I am afraid that suggestion will simply replace all ' marks with \, so it won't do what Celinio wanted.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually that will replace ' with '
So you are back to square one..
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems I am wrong...
for "java'ranch".replace("\","\\'");
the result seems to be...


\'j\'a\'v\'a\''\'r\'a\'n\'c\'h\'
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like I am wrong again and Campbell was right..

"java'ranch".replace("\'", "\\"); Got the arguments wrong..

gives..

java\ranch

sorry guys for the mess...
 
reply
    Bookmark Topic Watch Topic
  • New Topic