Keavon Keavon - 1 year ago 75
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download