Replacing all the '\' chars to '/' with C#

后端 未结 7 1934
春和景丽
春和景丽 2020-12-11 02:16

How can I replace all the \'\\\' chars in a string into \'/\' with C#? For example, I need to make @\"c:/abc/def\" from @\"c:\\abc\\def\".

相关标签:
7条回答
  • 2020-12-11 03:06
    string result = @"c:\asb\def".Replace(Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar);
    
    0 讨论(0)
  • 2020-12-11 03:10

    The Replace function seems suitable:

    string input = @"c:\abc\def";
    string result = input.Replace(@"\", "/");
    

    And be careful with a common gotcha:

    Due to string immutability in .NET this function doesn't modify the string instance you are invoking it on => it returns the result.

    0 讨论(0)
  • 2020-12-11 03:12
    string first = @"c:/abc/def";
    string sec = first.Replace("/","\\");
    
    0 讨论(0)
  • 2020-12-11 03:12
    @"C:\abc\def\".Replace(@"\", @"/");
    
    0 讨论(0)
  • 2020-12-11 03:14
    var replaced = originalStr.Replace( "\\", "/" );
    
    0 讨论(0)
  • 2020-12-11 03:15

    You need to escape the \

    mystring.Replace("\\", "/");
    
    0 讨论(0)
提交回复
热议问题