-5

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    Is it always in the same place in the URL (always right after `/app/` or always in the second position after the domain)? Or. Perhaps, always the only entirely numeric chunk of the URL – Flydog57 Oct 30 '21 at 20:04
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Please show your attempts you have tried and the problems/error messages you get from your attempts. – Progman Oct 30 '21 at 20:04
  • What is the use case? I feel like there are many libraries and frameworks that can handle this already and it need not be handled by yourself. XY Problem. Also, attempted code is required in the case you still need to implement this yourself. – Omar Abdel Bari Oct 30 '21 at 20:14

4 Answers4

3

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];
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

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;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Johannes Mols
  • 890
  • 1
  • 12
  • 35
  • Good to hear. Can you please accept the answer with the checkmark if it solved your problem? That would help others to find the right solution if they have the same question. – Johannes Mols Oct 31 '21 at 07:48
0
  1. Identify the begining, and the ending string marks.
  2. Beginning = "/app/", Ending = "/".
  3. Cut it out of there.


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

  1. Always try to write the simplest code you can get. And have fun!
TomeeNS
  • 455
  • 4
  • 10
  • That's not exactly the simplest code though... – Johannes Mols Oct 31 '21 at 07:50
  • What a useful comment, thank you. I've posted it for fun as a variation. Actually we do not know all the directives, and URLs are often unstable, as well as servers, domains and folders are prone to change. :-) – TomeeNS Oct 31 '21 at 09:19
  • I meant no offense with my comment. And you are right about the uncertainties of URL's, which is why I think your solution is not good. If the "app" part is renamed, you have to change it and recalculate the index offset. That's not pleasant to maintain. My solution uses the standard Uri class which takes the complexity away. – Johannes Mols Oct 31 '21 at 09:41