0

I have a string like this:

/Location/12345/

But I only need the number 12345.

The following function returns the number:

    private int GetNumberFromString(string location = "/Location/12345/")
    {
        var id = 0;
        var strings = location.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
        strings.FirstOrDefault(t => int.TryParse(t, out id));
        return id;
    }

I would like to do something like this:

    private int GetNumberFromString(string location = "/Location/12345/")
    {
        var strings = location.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
        return strings.FirstOrDefault(t => int.TryParse(t, out var id));
    }

But that returns string and not int.

So is it possible to declare the variable id inside of int.TryParse and return it immediately? Or is there a better solution for this?

Tobias R.
  • 47
  • 1
  • 7
  • _"I would like to do something like this"_ - why? You're returning the first string where `int.TryParse()`'s return value is `true`, not its `out int id`. What problem are you trying to solve? What "better solution" are you looking for? Shorter code isn't always better. – CodeCaster Sep 08 '17 at 15:16
  • Are you using c# 7.0? – Vijayanath Viswanathan Sep 08 '17 at 15:19
  • "Is there a better solution"? Well, what you're asking for doesn't make much sense -- for one, why require declaring an inline `out` parameter? Anyways, extracting a specific pattern from a string is generally solved with regular expressions: `Convert.ToInt32(Regex.Match("/Location/12345/", @"\d+").Groups[0].Value)` – BurnsBA Sep 08 '17 at 15:23
  • alternatively, `("/Location/12345/").Split('/').Select(x => { int i; return int.TryParse(x, out i) ? (int?)i : (int?)null; }).FirstOrDefault(x => x != null)` – BurnsBA Sep 08 '17 at 15:55
  • Well, my thought was, you can declare a variable inside of int.TryParse, so how do I return it, because it´s the last thing i´m doing -> searching a number. @CodeCaster I used int.TryParse inside FirstOrDefault because of its return value. I hoped that i could use id – Tobias R. Sep 11 '17 at 15:45

2 Answers2

0

With a regex ?

return int.Parse(new Regex("\\d+").Match("/location/1234/").Groups[0].Value)
GGO
  • 2,678
  • 4
  • 20
  • 42
  • 1
    For an actual answer, I might suggest a more restrictive pattern like `@"/[^/]+/(\d+)/"`. You'll also need to change the `Groups[0]` to `Groups[1]` – BurnsBA Sep 08 '17 at 15:28
-1

Not the best solution I'm sure, but it works.

private int GetNumberFromStringLinq(string location = "/Location/12345/")
        {
            var strings = location.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            return Convert.ToInt32(strings.FirstOrDefault(t => int.TryParse(t, out var id)));
        }
gilliduck
  • 2,762
  • 2
  • 16
  • 32
  • 1
    Yeah so now they first try to parse an int, then convert the string to an int when that succeeds. – CodeCaster Sep 08 '17 at 15:17
  • I said it wasn't good, but it does work. The core issue they have needs redone, but to answer their specific question, this does it. – gilliduck Sep 08 '17 at 15:18