How to Trim last string

Posted by Jayakumars under ASP.NET AJAX on 4/14/2014 | Points: 10 | Views : 454 | Status : [Member] [MVP] | Replies : 1
hi

my string like this

http://localhost:3617/Main/User

how to remove last index slash value i need output this

http://localhost:3617/Main

Mark as Answer if its helpful to you


Responses

Posted by: A2H on: 4/14/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can make use of the below code to remove the last section from your url.

var uri = new Uri("http://localhost:3617/Main/User");

var noLastSegment = string.Format("{0}://{1}", uri.Scheme, uri.Authority);

for(int i = 0; i < uri.Segments.Length - 1; i++)
{
noLastSegment += uri.Segments[i];
}
// Remove the last portion
noLastSegment = noLastSegment.Trim("/".ToCharArray()); // This will given you http://localhost:3617/Main


You can find a sample working demo code in below link
http://dotnetfiddle.net/dDnu2t

Thanks,
A2H
My Blog

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response