Naval Navc Naval Navc - 3 months ago 8
C# Question

Get Substring from Url

I have some Urls string, such as:

http://www.testproject.com/tokyo/4

http://www.testproject.com/india/11

http://www.testproject.com/singapore/819

How to get the number ("4". "11", "819") in the end of Url?

Answer

The other answers using string methods would be correct if this was a simple string, but since this is a URL, you should use the proper class to handle URIs:

var url = new Uri("http://www.testproject.com/tokyo/4");
var lastSegment = url.Segments.Last();