Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created May 13, 2024 15:33
Show Gist options
  • Save karenpayneoregon/ac79c65da31a1245b5f10b0bd4e4236b to your computer and use it in GitHub Desktop.
Save karenpayneoregon/ac79c65da31a1245b5f10b0bd4e4236b to your computer and use it in GitHub Desktop.
Extract multiple numbers from a string (C#)

Output

Number: 05 
Number: 40 
Number: 30 
Number: 10 
Number: 44 
string input = "There are 5 numbers in this string: 40, 30, and 10. 44";
string[] numbers = Regex.Split(input, @"\D+");
foreach (string value in numbers)
{
if (string.IsNullOrEmpty(value)) continue;
if (int.TryParse(value, out var number))
{
Debug.WriteLine($"Number: {number,-3:D2}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment