Home > OS >  Double separator in the path after Path.GetDirectoryName method c#
Double separator in the path after Path.GetDirectoryName method c#

Time:03-17

I want get directory from blob absolute Uri: https://001.blob.core.windows.net/files/11-files.trg.

For this I use Path.GetDirectoryName method. As result I have: https:\\001.blob.core.windows.net\\files.

Why does a double separator appear and how to replace it with a single one?

CodePudding user response:

I think Path.GetDirectoryName only works for relative paths, but not for URLs. So the first thing that comes to my mind is something like this:

var url = "https://001.blob.core.windows.net/files/11-files.trg";

var temp = new string(url.ToCharArray().Reverse().ToArray());
int index = temp.IndexOf('/');
temp = temp.Substring(index   1 , temp.Length - index - 1);
var result = new string(temp.ToCharArray().Reverse().ToArray());

Console.WriteLine(result);
//output: https://001.blob.core.windows.net/files
  • Related