Getting rid of String.Split does not leave many parameters in the table. One option is Regex.Matches , as shown by spender , and the other is Regex.Split (which does not give us anything new).
Instead of grouping, you can use any of these approaches:
var target = src.Split(new[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries); var result = target.Distinct() .Select(s => new { Word = s, Count = target.Count(w => w == s) });
A Distinct call is necessary to avoid duplication of elements. I went ahead and expanded the characters to smash them to get actual words without punctuation. I found that the first approach is the fastest using benchmarking code using spender.
Back to the requirement to order results from your previously asked question, you can easily expand the first approach as follows:
var result = target.Distinct() .Select(s => new { Word = s, Count = target.Count(w => w == s) }) .OrderByDescending(o => o.Count);
EDIT: got rid of Tuple since the anonymous type was at hand.
source share