I am trying to extract the number portion in this filename. "Name, lastname_123456_state_city.pdf"
I have got this far..
idstring = file.Substring(file.IndexOf("_") + 1,
(file.LastIndexOf("_") - file.IndexOf("_") - 1));
This is one of those cases where a regex might be better:
_(\d+)_
And, here is how you would use it
string input = "Name, lastname_123456_state_city.pdf";
string regexPattern = @"_(\d+)_";
Match match = Regex.Match(input, regexPattern, RegexOptions.IgnoreCase);
if (match.Success)
string yourNumber = match.Groups[1].Value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With