I have a URL string that is something like below:
https://example.com/app/1095600/example2234
I want to only get "1095600"
this number is variable and can be three or more digits long.
I have a URL string that is something like below:
https://example.com/app/1095600/example2234
I want to only get "1095600"
this number is variable and can be three or more digits long.
If the number is always in the same position (following https://example.com/app/
), you could split the string by the slash (/
) character and extract it:
string input = "https://example.com/app/1095600/example2234";
string result = input.Split("/")[4];
You can try matching the required substring with a help of regular expressions, esp. if you have elaborated criteria
three or more digits
Code:
using System.Text.RegularExpressions;
...
string url = "https://example.com/app/1095600/example2234";
string number = Regex.Match(url, @"(?<=\/)[0-9]{3,}(?=\/|$)").Value;
Mureinik's answer will work fine, but can quickly break if your URL is missing the https://
part. It would be much better to convert the string to a Uri
and use the Uri.Segments property to extract the second segment of the path.
Uri address = new Uri("https://example.com/app/1095600/example2234");
string id = address.Segments[2].Replace("/", "");
The segments include the ending slash, so you need to remove it manually.
String input = "https://example.com/app/1095600/example2234";
String str_number = input.Substring(input.IndexOf("/app/")+5).Split('/')[0];
int int_number = Convert.ToInt32(str_number);