[Solved] Removing some characters from string in c# [closed]


Here is one way:

  1. Create a System.Uri from the string
  2. Break it apart into .Segments
  3. Drop the last segment (the filename). (with .Take(length-1) or .SkipLast(1))
  4. Concatenate the remaining segments back together with string.Join
  5. 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]