Getting rid of String.Split
doesn't leave many options on the table. One option is Regex.Matches
as spender demonstrated, and another is Regex.Split
(which doesn't give us anything new).
Rather than grouping you could use either 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) });
// or dictionary approach
var result = target.Distinct()
.ToDictionary(s => s, s => target.Count(w => w == s));
The Distinct
call is needed to avoid duplicate items. I went ahead and expanded the characters to split on to get the actual words devoid of punctuation. I found the first approach to be the quickest using spender's benchmarking code.
Back to the requirement to order the results from your previously referenced question, you could easily extend the first approach as follows:
var result = target.Distinct()
.Select(s => new { Word = s, Count = target.Count(w => w == s) })
.OrderByDescending(o => o.Count);
// or in query form
var result = from s in target.Distinct()
let count = target.Count(w => w == s)
orderby count descending
select new { Word = s, Count = count };
EDIT: got rid of the Tuple since the anonymous type was close at hand.