Solved

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

Posted on 2007-11-22
12
251 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
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 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
 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 32
REXEX help Part 2 2 25
VB.Net Tranfer data between to VB.Net apps 8 26
Need help deploying my first MVC.Net app with a SQL Server backend 3 13
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

821 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