2013-05-13 151 views
0

我想用"\"(两个换成一个)替换"\\"。 我使用:如何用“String”替换“”为String.Replace()

string myPath = Path.GetFullPath(fileName);  
string correctPath = myPath.Replace(@"\\", @"\"); 

但什么也没有发生,在correctPath字符串继续"\\"

+3

您是否在调试器中查看它 - 它会显示转义字符串... – 2013-05-13 16:07:33

+0

即使使用verbatin? – 2013-05-13 16:08:34

+0

你还有其他示例代码吗?你需要做一些事情: 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

回答

10

你可能看弦,同时暂停在调试器。将值打印到控制台窗口,没关系。

string myPath = @"hello\\world"; 
string correctPath = myPath.Replace(@"\\", @"\"); 
Console.Write(correctPath); 
Console.Read(); 
+1

打我吧!我只是写出了这个确切的代码,它绝对有效。 – valverij 2013-05-13 16:08:28

+0

工作,谢谢。 (我需要等待10分钟才能接受此答案) – 2013-05-13 16:10:19

+0

样本中的字符串不包含两个反斜杠。只有一个。你是不是指'@“hello \\ world”'? – 2013-05-13 16:11:33

相关问题