Solved

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

Posted on 2007-11-22
12
249 Views
Last Modified: 2013-12-16
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
Comment
Question by:stevejebson
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 20336087
You have to backslash the backslash

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

is proper.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20336098
this will work too:
strAddress.Replace(@"'",@"\")
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20336107
also will work:
strAddress.Replace((char)39, (char)92);
0
 
LVL 1

Author Comment

by:stevejebson
ID: 20336199
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
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 20336205
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20336206
Are you using the RETURN value from your call to Replace()?...

    strAddress = strAddress.Replace("x", "y");
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20336208
*Just calling Replace on the string doesn't actually change the string!
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 20336213
Since he is getting DIFFERENT values than the starting values, he must be using return values.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20336231
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
 
LVL 1

Author Comment

by:stevejebson
ID: 20336282
that's it Jaime, why would the debugging window  lie ??
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20336289
it doesn't lie, just is showing you the string in "C-language" style notation.
0
 
LVL 1

Author Closing Comment

by:stevejebson
ID: 31410601
thanks very much, i coul have been on this for days ;-)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now