Here is one way:
- Create a
System.Uri
from the string - Break it apart into
.Segments
- Drop the last segment (the filename). (with
.Take(length-1)
or.SkipLast(1)
) - Concatenate the remaining segments back together with
string.Join
- Drop the trailing
/
using.TrimEnd
.NET Framework:
var uri = new Uri("Https://example.com/assets/css/bootstrap.css");
string result = string.Join("", uri.Segments.Take(uri.Segments.Length-1)).TrimEnd("https://stackoverflow.com/");
.NET Standard:
string result = string.Join("", new Uri("Https://example.com/assets/css/bootstrap.css")
.Segments.SkipLast(1)).TrimEnd("https://stackoverflow.com/");
solved Removing some characters from string in c# [closed]