[英]How can I replace all instances of the backslash character?
我试图将将用于RichTextBox的值插入mysql字段。 由于mysql从字面上不会处理\\\\
或\\r
,因此我想用\\\\
替换所有出现的\\
。 我努力了:
mystring.Replace(@"\", @"\\");
但不适用于\\r
, \\n
等。我还能怎么做?
编辑:我的输入看起来像:
\\ RTF1 \\ ANSI \\ ansicpg1252 \\ deff0 \\ r \\ n
和我的输出应如下所示:
\\\\ \\\\ RTF1 ANSI \\\\ \\\\ ansicpg1252 deff0 \\ r \\ n
谢谢。
尝试:
Regex.Escape(mystring);
请参阅: 使用C#和ASP.Net在MySQL中转义特殊字符
例:
var data = "this is \n a \r\ntest";
var result = Regex.Escape(data); // this\ is\ \n\ a\ \r\ntest
//replace escaped spaces
Console.Write(result.Replace(@"\ ", " ")); // this is \n a \r\ntest
这对我有用:
var mystring = @"\rtf1\ansi\ansicpg1252\deff0\r\n";
mystring = mystring.Replace(@"\", @"\\");
产量
\\rtf1\\ansi\\ansicpg1252\\deff0\\r\\n
我唯一能想到的是:
mystring
转义? mystring
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.