I am at a loss as to how to do this. I am printing some information to a richtextbox that will be multiple lines, has words and numbers. I need to search the richtextbox for spe
Iterate array and use Int32.TrParse method to determine if the "word" is in fact a number
var input = "User: Matt User's number: 10";
int num = 0;
foreach(var word in input.Split(' '))
{
if (Int32.TryParse(word, out num) && Enumerable.Range(1,30).Contains(num))
{
Console.WriteLine("The user number is " + num);
break;
}
}
or with linq:
int testNum;
var digits = input.Split(' ').Where(a => Int32.TryParse(a, out testNum) && Enumerable.Range(1, 30).Contains(testNum)).FirstOrDefault();
Console.WriteLine("Linq The user number is " + (!string.IsNullOrEmpty(digits) ? digits : "Not Found"));