如何对某个词在字符串中出现的次数进行计数 (LINQ) (C#)

此示例演示如何使用 LINQ 查询对指定词在字符串中出现的次数进行计数。 请注意,若要执行计数,首先需调用 Split 方法来创建词数组。 Split 方法存在性能开销。 如果只需要统计字符串的字数,则应考虑改用 MatchesIndexOf 方法。 但是,如果性能不是关键问题,或者已拆分句子以对其执行其他类型的查询,则使用 LINQ 来计数词或短语同样有意义。

示例

class CountWords  
{  
    static void Main()  
    {  
        string text = @"Historically, the world of data and the world of objects" +  
          @" have not been well integrated. Programmers work in C# or Visual Basic" +  
          @" and also in SQL or XQuery. On the one side are concepts such as classes," +  
          @" objects, fields, inheritance, and .NET APIs. On the other side" +  
          @" are tables, columns, rows, nodes, and separate languages for dealing with" +  
          @" them. Data types often require translation between the two worlds; there are" +  
          @" different standard functions. Because the object world has no notion of query, a" +  
          @" query can only be represented as a string without compile-time type checking or" +  
          @" IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to" +  
          @" objects in memory is often tedious and error-prone.";  
  
        string searchTerm = "data";  
  
        //Convert the string into an array of words  
        string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);  
  
        // Create the query.  Use the InvariantCultureIgnoreCase comparision to match "data" and "Data"
        var matchQuery = from word in source  
                         where word.Equals(searchTerm, StringComparison.InvariantCultureIgnoreCase)  
                         select word;  
  
        // Count the matches, which executes the query.  
        int wordCount = matchQuery.Count();  
        Console.WriteLine("{0} occurrences(s) of the search term \"{1}\" were found.", wordCount, searchTerm);  
  
        // Keep console window open in debug mode  
        Console.WriteLine("Press any key to exit");  
        Console.ReadKey();  
    }  
}  
/* Output:  
   3 occurrences(s) of the search term "data" were found.  
*/  

编译代码

使用 System.Linq 和 System.IO 命名空间的 using 指令创建 C# 控制台应用程序项目。

请参阅