2014-09-30 252 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(@"""", @"\""")); 
相關問題