小编use*_*027的帖子

计算文本文件中的单词数

我正在尝试计算文本文件中的单词数,即开始.

这是对字数统计程序的测试.这只是一个测试.如果您的程序成功运行,则应计算此文件中有30个单词.

我正在使用StreamReader将文件中的所有内容放入一个字符串中,然后使用.Split方法获取单个单词的数量,但在编译和运行程序时,我一直得到错误的值.

using System;
using System.IO;

class WordCounter
{
    static void Main()
    {
        string inFileName = null;

        Console.WriteLine("Enter the name of the file to process:");
        inFileName = Console.ReadLine();

        StreamReader sr = new StreamReader(inFileName);

        int counter = 0;
        string delim = " ,.";
        string[] fields = null;
        string line = null;

        while(!sr.EndOfStream)
        {
            line = sr.ReadLine();
        }



        fields = line.Split(delim.ToCharArray());
        for(int i = 0; i < fields.Length; i++)
        {
            counter++;
        }
        sr.Close();
        Console.WriteLine("The word count is {0}", counter);
    }
} 
Run Code Online (Sandbox Code Playgroud)

.net c# regex

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

.net ×1

c# ×1

regex ×1