I am trying to filter a List of strings based on the number of words in each string. I am assuming that you would trim any white-space at the ends of the string, and then count the number of spaces left in the string, so that WordCount = NumberOfSpaces + 1. Is that the most efficient way to do this? I know that for filtering based on character count the following is working fine...just cant figure out how to write it succinctly using C#/LINQ.
if (checkBox_MinMaxChars.Checked)
{
int minChar = int.Parse(numeric_MinChars.Text);
int maxChar = int.Parse(numeric_MaxChars.Text);
myList = myList.Where(x =>
x.Length >= minChar &&
x.Length <= maxChar).ToList();
}
Any ideas of for counting words?
UPDATE: This Worked like a charm...Thanks Mathew:
int minWords = int.Parse(numeric_MinWords.Text);
int maxWords = int.Parse(numeric_MaxWords.Text);
sortBox1 = sortBox1.Where(x => x.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Count() >= minWords &&
x.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Count() <= maxWords).ToList();