Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-22
12
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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 86

Expert Comment

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

    strAddress = strAddress.Replace("x", "y");
0
 
LVL 86

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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