The shortest way to write. is the use of regex. he will find matches for you. just get the counts. Also the regex ignores the case variant, so you don't need to use ToLowerfor a large string. So after reading the file
string aliceFile = Path.Combine(Environment.CurrentDirectory, "bestanden\\alice_in_wonderland.txt");
string text = File.ReadAllText(aliceFile);
Regex r = new Regex("queen", RegexOptions.IgnoreCase);
var count = r.Matches(input).Count;
Also, since the input is very large, but the template is simple, you can use RegexOptions.Compiledit to make things faster.
Regex r = new Regex("queen", RegexOptions.IgnoreCase | RegexOptions.Compiled);
var count = r.Matches(input).Count;
source
share