Home > Blockchain >  Getting a number part from a url string in c#
Getting a number part from a url string in c#

Time:10-31

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.

CodePudding user response:

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];

CodePudding user response:

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;
  • Related