【问题标题】:C# - How to count Words and letters from a .RTF / .TXT File?C# - 如何计算 .RTF / .TXT 文件中的单词和字母?
【发布时间】:2011-08-19 08:47:04
【问题描述】:

标题说明一切..

搜索了谷歌和 Stackoverflow,没有找到类似的东西..

【问题讨论】:

    标签: c# .net regex text


    【解决方案1】:

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

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

    计算字母:

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

    【讨论】:

    • 谢谢,效果很好 - 数单词.. 现在只需要字母:D
    • 我的解决方案:私有 int GetLetterCount() { int count = 0; StreamReader tr = new StreamReader("hello.txt");字符串文本 = tr.ReadToEnd(); foreach (char c in text) { if (!char.IsWhiteSpace(c)) { count++; } } 返回计数; }
    • 您的方法将计算数字、标点符号、字母,即不包括空格的所有字符。
    【解决方案2】:
    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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-28
        • 2020-10-04
        • 2015-06-02
        • 1970-01-01
        • 1970-01-01
        • 2017-04-03
        • 2021-06-25
        • 2012-10-16
        相关资源
        最近更新 更多