首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用C#计算段落中某个单词的数量

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

Stack Overflow用户
提问于 2015-02-15 04:10:29
回答 3查看 1.9K关注 0票数 1

我正在试着写一个程序,用户给系统一个单词和一个段落,系统的工作是计算这个单词出现的次数。

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

EN

回答 3

Stack Overflow用户

发布于 2015-02-15 04:37:16

将正则表达式与Word Boundary锚点一起使用:

代码语言:javascript
运行
复制
int wordCount = Regex.Matches(text, "\\b" + Regex.Escape(searchTerm) + "\\b", RegexOptions.IgnoreCase).Count;
票数 4
EN

Stack Overflow用户

发布于 2015-02-15 04:16:51

正如文章中所说的“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循环并进行计数。

代码语言:javascript
运行
复制
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.
*/
票数 1
EN

Stack Overflow用户

发布于 2015-02-15 04:12:03

代码语言:javascript
运行
复制
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();

这将:

通过检查每个字符( Count() ),

  • 将每个标点符号替换为一个空格,并将它们作为另一个字符串重新组合在一起,以将字符串放入由空格分隔的子字符串中,以便只保留那些与test.select search
  • Count them匹配的字符串
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28519785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档