C#用1char替换1+出现的char

时间:2016-04-21 17:47:05

标签: c# .net visual-studio

我不知道如何取代" \"只有1" \"

的路径

示例:

C:\\\ProgramFiles\\New Folder\TestFolder\\Test\\\

并在更换后应该像:

C:\ProgramFiles\New Folder\TestFolder\Test\

但是可能会出现更多的" \"比3

1 个答案:

答案 0 :(得分:2)

您可以使用正则表达式将任意数量的连续\个字符与使用Regex.Replace()方法的单个字符进行匹配:

// This will replace any number of consecutive slashes with a single slash
var output = Regex.Replace(input,@"\\+","\\");

你可以see a working example of this here

此外,如果您在Visual Studio调试器中查看这些路径,那么值得注意的是它们通常会与其关联的转义字符(额外的\)一起出现,这可能实际上并不存在在最后的字符串本身。

相关问题