String.Replace() replce ' with \' not working

Any ideas why strAddress.Replace("'","\'") doesn't work, it works with double backslash or any other text string but i just need to replace the quote with a backslash and quote

thanks
LVL 1
Steve JebsonDeveloperAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
it doesn't lie, just is showing you the string in "C-language" style notation.
0
 
Jens FiedererTest Developer/ValidatorCommented:
You have to backslash the backslash

strAddress.Replace("\"", "\\\"")

is proper.
0
 
Jaime OlivaresSoftware ArchitectCommented:
this will work too:
strAddress.Replace(@"'",@"\")
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Jaime OlivaresSoftware ArchitectCommented:
also will work:
strAddress.Replace((char)39, (char)92);
0
 
Steve JebsonDeveloperAuthor Commented:
ok, maybe i'm doing something real stupid here 'cos none of these worked....

strAddress contains "St Jame's Place"

strAddress.Replace("\"", "\\\"")  gives me  "St Jame\\\'s Place"

strAddress.Replace(@"'",@"\")  gives me "St Jame\\'s Place"

and i can't get the syntax right with the (char)39,string.concat((char)92,(char)39)

0
 
Jens FiedererTest Developer/ValidatorCommented:
OK, it's hard to see these quotes on the screen - I thought you were trying to convert a DOUBLE QUOTE, not a single quote.


Try  

strAddress.Replace("'", "\\'")
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you using the RETURN value from your call to Replace()?...

    strAddress = strAddress.Replace("x", "y");
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*Just calling Replace on the string doesn't actually change the string!
0
 
Jens FiedererTest Developer/ValidatorCommented:
Since he is getting DIFFERENT values than the starting values, he must be using return values.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Maybe you are looking the value of the string in the debugger windows. This could be confusing. Try to do this:

System.Windows.Forms.MessageBox.Show(strAddress);

to watch the string in a Window.
0
 
Steve JebsonDeveloperAuthor Commented:
that's it Jaime, why would the debugging window  lie ??
0
 
Steve JebsonDeveloperAuthor Commented:
thanks very much, i coul have been on this for days ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.