C# - 如何计算.RTF / .TXT文件中的单词和字母?

时间:2011-08-19 08:47:04

标签: c# .net regex text

标题说明了一切..

搜索了Google和Stackoverflow,但没有找到类似的内容..

3 个答案:

答案 0 :(得分:3)

对于.txt文件,您可以使用regexp \b\w+\b。它将匹配所有出现的单词,例如:

var count = Regex.Matches(input, @"\b\w+\b").Count;

计算字母:

int count = input.Count(char.IsLetter);

答案 1 :(得分:1)

static void Main()
{
     const string t1 = "To be or not to be, that is the question.";
     Console.WriteLine(WordCounting.CountWords1(t1));
     Console.WriteLine(WordCounting.CountWords2(t1));

     const string t2 = "Mary had a little lamb.";
     Console.WriteLine(WordCounting.CountWords1(t2));
     Console.WriteLine(WordCounting.CountWords2(t2));
}

更多是here

答案 2 :(得分:0)

从rtf获取文字 - Get plain text from an RTF text

计算文字中的字词 - http://www.dotnetperls.com/word-count