Reputation: 209
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?
Upvotes: 1
Views: 5960
Reputation: 2214
int LastFwdSlash = URLString.LastIndexOf('/');
String Number = URLString.Substring(LastFwdSlash + 1, URLString.Length);
int Number = int.Parse(URLString);
Upvotes: 0
Reputation: 7421
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();
Upvotes: 12
Reputation: 65097
Example of what AD.Net meant:
public string getLastBit(string s) {
int pos = s.LastIndexOf('/') + 1;
return s.Substring(pos, s.Length - pos);
}
Returns:
4, 11, 819
When passed in an individual url.
Upvotes: 2
Reputation: 13399
Without using regex, you can find the index of last "/" by using string.LastIndexOf('/')
and get the rest by using string.SubString
Upvotes: 3
Reputation: 317
string myUrl = "http://www.testproject.com/tokyo/4";
string[] parts = myUrl.Split('/');
string itIsFour = parts[parts.Length-1];
Upvotes: 0