2014-09-30 189 views
-1

我试图逃跑\"在我的字符串是这样的:C#转义字符串双引号

text.Replace("\\", "\\\\").Replace("\"", "\\\""); 

但结果文本

arash "moeen" 

原来作为

arash \\\"moeen\\\" 

任何人都可以帮助我吗?提前致谢。

+0

什么是“文字”在开始?你用\\替换\,用'\“替换'”'。 – 2014-09-30 07:37:24

+0

你为什么需要逃避它们? – zerkms 2014-09-30 07:38:21

+0

text = arash“moeen” – 2014-09-30 07:38:23

回答

1

只使用@作为逐字字符串。

text.Replace(@"this", @"that"); 

例子:

text.Replace(@"\", @"\\").Replace(@"""", @"\"""); 
+0

并且使用双引号引起来:即得到'a“b”'你输入'@“a”“b”“”''。 – 2014-09-30 07:41:14

+0

所以为逃避“应该怎么样?.replace(@”\“”,@“\\\”“)?? – 2014-09-30 07:42:34

+1

它应该是text.Replace(@”\“,@”\\“)。 (@“”“”,@“\”“”) – Alexey 2014-09-30 07:45:11

1

什么是你的工作分配的代码?

应该

var text = @"arash ""moeen"""; 
2

如果我理解正确的话,首先,text = arash "moeen"不是有效的规则字符串。我假设你的字符串像;

string s = "text = arash \"moeen\""; 

其打印为

text = arash "moeen" // I think this is your original string. 

既然你arash \"moeen\"结果,你只需要在你的字符串像\"更换您";

string s = "text = arash \"moeen\""; 
s = s.Replace("\"", "\\\""); 

所以你的结果将是arash \"moeen\"

的更多信息:Escape Sequences

如果这是一个JSON字符串,我的回答将是无效的:-P

+0

是的,它将作为JSON字符串的一部分,它会在我解析它之后在我的textview中结束arash \“moeen \” – 2014-09-30 08:03:26

-1

当U想arash \"moeen\"arash "moeen"text.Replace(", \");

+0

我不认为'text.Replace(“,\”)'将起作用 – 2014-09-30 07:44:50

+0

当你做字符串text =“arash”moeen“”然后text.Replace(“,\”); – Sybren 2014-09-30 07:49:03

+0

@Sybren甚至不会编译。没有重载string.Replace接受一个string类型的参数。 – 2014-09-30 09:48:59

1
string text = @"arash ""moeen"""; 
MessageBox.Show(text.Replace(@"\", @"\\").Replace(@"""", @"\""")); 
相关问题