我想用"\"
(兩個換成一個)替換"\\"
。 我使用:如何用「String」替換「」爲String.Replace()
string myPath = Path.GetFullPath(fileName);
string correctPath = myPath.Replace(@"\\", @"\");
但什麼也沒有發生,在correctPath字符串繼續"\\"
我想用"\"
(兩個換成一個)替換"\\"
。 我使用:如何用「String」替換「」爲String.Replace()
string myPath = Path.GetFullPath(fileName);
string correctPath = myPath.Replace(@"\\", @"\");
但什麼也沒有發生,在correctPath字符串繼續"\\"
你可能看弦,同時暫停在調試器。將值打印到控制檯窗口,沒關係。
string myPath = @"hello\\world";
string correctPath = myPath.Replace(@"\\", @"\");
Console.Write(correctPath);
Console.Read();
打我吧!我只是寫出了這個確切的代碼,它絕對有效。 – valverij 2013-05-13 16:08:28
工作,謝謝。 (我需要等待10分鐘才能接受此答案) – 2013-05-13 16:10:19
樣本中的字符串不包含兩個反斜槓。只有一個。你是不是指'@「hello \\ world」'? – 2013-05-13 16:11:33
您是否在調試器中查看它 - 它會顯示轉義字符串... – 2013-05-13 16:07:33
即使使用verbatin? – 2013-05-13 16:08:34
你還有其他示例代碼嗎?你需要做一些事情: const string s =「Darth Vader是可怕的。」; \t Console.WriteLine(s); \t //注意: \t //您必須將Replace的結果賦給一個新的字符串。 \t string v = s.Replace(「scary」,「not scary」); \t Console.WriteLine(v); – Sean 2013-05-13 16:09:18