Remove Extra back slash “\” from string file path in c#

后端 未结 3 1468
鱼传尺愫
鱼传尺愫 2021-01-02 14:13

How to convert

\"String path = @\"C:\\Abc\\Omg\\Why\\Me\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";

into

String path = @\"C:\\A

相关标签:
3条回答
  • 2021-01-02 14:39
    var path = @"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\";
    path = path.TrimEnd('\\') + '\\';
    

    another solution is

    var path = @"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\";
    path = Path.GetFullPath(path);
    
    0 讨论(0)
  • 2021-01-02 14:50

    You can just construct path using the Path static class:

    string path = Path.GetFullPath(@"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\");
    

    After this operation, variable path will contain the minimal version:

    C:\Abc\Omg\Why\Me\
    
    0 讨论(0)
  • 2021-01-02 14:55

    You can use path.TrimEnd('\\'). Have a look at the documentation for String.TrimEnd.

    If you want the trailing slash, you can add it back easily.

    0 讨论(0)
提交回复
热议问题