user482375
user482375

Reputation:

Replace Quote with Escape and Quote

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

Answers (6)

Shyju
Shyju

Reputation: 218932

This works

string s = "diana\"s here";
string s2=s.Replace ("\"","\\\"");

Upvotes: 0

T23
T23

Reputation: 572

Lazy, but i'm almost sure it works.

Replace("\"","\\" + "\"");

Upvotes: 0

Mitch
Mitch

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

Randolpho
Randolpho

Reputation: 56448

You have to escape the backslash as well as the quote:

mystring.Replace("\"", "\\\"")

Upvotes: 2

Episodex
Episodex

Reputation: 4559

.Replace("\"", "\\\"")

\\ means \ character. You must escape it too, so it can be shown.

Upvotes: 2

Brad Christie
Brad Christie

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

Related Questions