如何使用C#计算段落中某个单词的数量 [英] How do you count the number of a certain word in a paragraph using C#

查看:44
本文介绍了如何使用C#计算段落中某个单词的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,用户在其中给系统一个单词和一个段落,系统的工作是计算该单词弹出的次数.

I'm trying to write a program where the user gives the system a word, and a paragraph, the system's job is to count how many times that word pops up.

我如何计算单词在C#中弹出多少次?

How can I count how many times the word pops up in C#?

推荐答案

将正则表达式与单词边界锚点:

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

这篇关于如何使用C#计算段落中某个单词的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆