• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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
0
Steve Jebson
Asked:
Steve Jebson
  • 4
  • 3
  • 3
  • +1
1 Solution
 
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
 
Jaime OlivaresSoftware ArchitectCommented:
also will work:
strAddress.Replace((char)39, (char)92);
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
Jaime OlivaresSoftware ArchitectCommented:
it doesn't lie, just is showing you the string in "C-language" style notation.
0
 
Steve JebsonDeveloperAuthor Commented:
thanks very much, i coul have been on this for days ;-)
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now