Using the
Uri class to resolve Uri, you can use the Segment property to access all segments and rebuild the Uri without the last segment.
var myString = "http://server/sitename/folder1/folder2/123_0.0";
var uri = new Uri(myString);
var noLastSegment = string.Format("{0}://{1}", uri.Scheme, uri.Authority);
for (int i = 0; i < uri.Segments.Length - 1; i++)
{
noLastSegment += uri.Segments[i];
}
noLastSegment = noLastSegment.Trim("/".ToCharArray()); // remove trailing `/`
Console.WriteLine(noLastSegment);
Participant
990 Points
475 Posts
Re: Remove last string after special character
Nov 21, 2017 08:02 AM|zxj|LINK
Hi Venkatzeus,
Using the Uri class to resolve Uri, you can use the Segment property to access all segments and rebuild the Uri without the last segment.
Regards,
zxj