Solved

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

Posted on 2007-11-22
12
250 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 48
Run time Error 4 35
Visual Studio TFS - how do I check in my code? 2 30
How to join on ID, with prefix? 15 0
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

861 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

24 Experts available now in Live!

Get 1:1 Help Now