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?
With a regex ?