[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-11-22
12
Medium Priority
?
263 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 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