Keavon Keavon - 2 months ago 13
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?

Answer

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.