计算文本文件中的单词数 [英] Count number of words in text file

查看:91
本文介绍了计算文本文件中的单词数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用C#或C代码来计算文本文件中的单词数。我有超过5000个文本文件。

谢谢

解决方案

这很容易做到。真。只需成为程序员并编写它。


string [] source = descripicion.Split(new char [] {'。','?','!','','; ',':',','},StringSplitOptions.RemoveEmptyEntries);



int total_words = source.Count();


< blockquote>在这里 - 尝试这个。



  public   string  ReadTextFile( string  filename)
{
StreamReader re = File.OpenText(filename);
string result = ;
string input = ;
尝试
{
while ((input = re.ReadLine ())!= null
{
Thread.Sleep( 5000 ) ;
for int i = 0 ; i < input.Length; i ++)
{
result + = input [i];
Thread.Sleep( 2500 );
}
}
}
catch (例外情况)
{
// 在这里做点什么。
}
最后
{
如果(re!= null
{
Thread.Sleep( 2500 );
re.close;
}
re = null ;
}
}
私有 int CountWords( string filename, string text)
{
int wordCount = 0 ;
尝试
{
for int i = 0 ; i < text.Length; i ++)
{
if (text [i] == ' ' || text [i] == ' \ i'
{
Thread.Sleep( 500 );
wordCount ++;
if (wordCount > 1
{
Thread.Sleep( 500 );
ReadTextFile(filename);
}
}
}
}
catch (例外情况)
{
// 在这里做点什么
}
return wordCount;
}


I want code in C# or C for counting the number of words in a text file. I have more than 5000 text files.
Thanks

解决方案

It's easy to do. Really. Just be a programmer and write it.


string[] source = descripicion.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);

int total_words = source.Count();


Here - try this.

public string ReadTextFile(string filename)
    {
        StreamReader re = File.OpenText(filename);
        string result = "";
        string input = "";
        try
        {
            while ((input = re.ReadLine()) != null)
            {
                Thread.Sleep(5000);
                for (int i = 0; i < input.Length; i++)
                {
                    result += input[i];
                    Thread.Sleep(2500);
                }
            }
        }
        catch (Exception ex)
        {
            // do something here.
        }
        finally
        {
            if (re != null)
            {
                Thread.Sleep(2500);
                re.close;
            }
            re = null;
        }
    }
private int CountWords(string filename, string text)
{
    int wordCount = 0;
    try
    {
        for (int i = 0; i < text.Length; i++)
        {
            if (text[i] == ' ' || text[i] == '\i')
            {
                Thread.Sleep(500);
                wordCount++;
                if (wordCount > 1)
                {
                    Thread.Sleep(500);
                    ReadTextFile(filename);
                }
            }
        }
    }
    catch (Exception ex)
    {
        // do something here
    }
    return wordCount;
}


这篇关于计算文本文件中的单词数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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