0

I would like to do a simple addslash to a string in c#.

My string is (for example) : "bla bla ' bla bla" And i would like the new string to be : "bla bla \' bla bla".

Unfortunately, when i do

str.Replace("'", "\'") or str.Replace("'", "\\'")

or a lot of other combination i end up with :

"bla bla \\' bla bla". I don't get it to just have one backslash.

Do you guys have some ideas ?

Thanks

4

1 回答 1

6

str.Replace("'", "\\'")是正确的用法。它将用单斜杠后跟单引号替换单引号。

由于字符串在 C# 调试器中的显示方式,if 看起来像两个斜杠,但如果您在某处(控制台、网页、winform、文件输出)显示字符串,它将只有一个斜杠。

于 2012-06-18T11:29:35.073 回答