LINQで単語を数える必要があります。これが私が長い文字列配列の単語を数えるために使用するコードですが、これはあまり効率的ではありません:
public static int WordCount(string haystack, string needle)
{
if (needle == null)
{
return 0;
}
string[] source = haystack.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',', '*', '-' }, StringSplitOptions.RemoveEmptyEntries);
var matchQuery = from word in source
where word.ToLowerInvariant() == needle.ToLowerInvariant()
select word;
int count=matchQuery.Count();
return count;
}
次のような文字列があるとします。
Geo Prism GEO 1995 GEO* - ABS #16213899 HGEO-
上記の文でGEOを見つけようとすると、ルーチンが正しいカウントを返しません。4を期待します。私のルーチンの何が問題になっていますか?