如何使用C#计算段落中某个单词的数量?

时间:2015-02-14 20:10:29

标签: c# counting words

我试图编写一个程序,用户给系统一个单词和一个段落,系统的工作是计算该单词弹出的次数。

如何计算C#中弹出的单词的次数?

3 个答案:

答案 0 :(得分:4)

使用带有Word Boundary锚点的正则表达式:

int wordCount = Regex.Matches(text, "\\b" + Regex.Escape(searchTerm) + "\\b", RegexOptions.IgnoreCase).Count;

答案 1 :(得分:1)

<强> https://msdn.microsoft.com/en-us/library/bb546166.aspx

正如文章所说“There is a performance cost to the Split method. If the only operation on the string is to count the words, you should consider using the Matches or IndexOf methods instead

所以你可以使用带有indexOf的while循环,并计算性能是否有问题。

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 Framework 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 ToLowerInvariant to match "data" and "Data"  
        var matchQuery = from word in source
                         where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
                         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.
*/

答案 2 :(得分:0)

String test = "the full full :full? text !!! ";
String search = "full";
int count = String.Concat(test.Select(i => Char.IsPunctuation(i) ? ' ' : i))
                  .Split(' ').Where(i => i == search).Count();

这将:

  • 通过检查每个字符(test.select)将每个标点符号替换为空格,并将它们再次组合为另一个字符串(String.Concat
  • 将字符串拆分为由空格(.Split
  • 分隔的子字符串
  • 过滤掉以仅保留与search字符串匹配的
  • 计算他们Count()