Keavon Keavon - 1 month ago 10x
C# Question

C# get sequences of numbers from string containing numbers and words?

Given a string like "10 of 15 points" or "0 of 1 point" how would I get the first number in one variable and the second number in a second variable?


You can use Split and LINQ:

var str = "10 of 15 points";
var numbers = str.Split().Where(x => x.All(char.IsDigit))
              .Select(x => int.Parse(x)).ToList();

This would return a List<int> that contains 10 and 15.