Reputation:
Dumb question probably, but can't seem to get it to work. I need to Replace quotes from a textbox with \" so it will export to excel correctly. I'm trying:
[Note].Text).Replace("\"", "\"")
Am I doing it completely wrong? Wouldn't surprise me if I am. Any recommendations on how to do this?
Thanks!
Upvotes: 8
Views: 11222
Reputation: 218932
This works
string s = "diana\"s here";
string s2=s.Replace ("\"","\\\"");
Upvotes: 0
Reputation: 399
Replace("\"", "\\\"");
You need 3 \ for the replacement string, the first one to escape the second one so that a \ will appear in the value, and the third one to escape the quotes
Upvotes: 0
Reputation: 56448
You have to escape the backslash as well as the quote:
mystring.Replace("\"", "\\\"")
Upvotes: 2
Reputation: 4559
.Replace("\"", "\\\"")
\\
means \
character. You must escape it too, so it can be shown.
Upvotes: 2
Reputation: 101614
String quotedText = "\"Hello, world!\"";
// quotedText = "Hello, World!"
String newQuotedText = quotedText.replace("\"", "\\\"");
// newQuotedText = \"Hello, World!\"
You need to escape the backslash you want transferred to the new value as well.
Upvotes: 15