2

我需要通过 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。我的日常有什么问题?

4

2 回答 2

5

Your code I think is almost right, but the reason you don't get 4 is because you need to do an "Contains" check since the last Geo is part of hgeo (assuming you meant to get 4, and not 3)

var matchQuery = from word in source
    where word.ToLowerInvariant.Contains(needle.ToLowerInvariant())
    select word;

Also, you might find it gives you better mileage to split your text with:

Regex.Split(haystack, @"\W+")

Which would split your text into a bunch of words, ignoring any grammar (untested, but I think should work)

于 2012-07-10T11:23:27.107 回答
5

您可以使用 LINQ 将其作为单线:

void Main()
{
    string data = "Geo Prism GEO 1995 GEO* - ABS #16213899 HGEO-";
    var target = "GEO";
    var count = data.Select((c, i) => data.Substring(i)).Count(sub => sub.ToUpper().StartsWith(target));
    Console.WriteLine(count.ToString());
}

结果:

4
于 2012-07-10T11:31:41.020 回答