C# 从文件中计算字符串中的单词

C# 从文件中计算字符串中的单词,c#,string,file-io,C#,String,File Io,我正在开发一个小型控制台应用程序,它返回几个0,而不是实际的字数。我还注意到,在某些方面,我的逻辑将有缺陷,因为我正在计算空间。这通常不会计算字符串中的最后一个单词。关于如何修复我的代码的任何建议。谢谢 static void Main() { bool fileExists = false; string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocum

我正在开发一个小型控制台应用程序,它返回几个0,而不是实际的字数。我还注意到,在某些方面,我的逻辑将有缺陷,因为我正在计算空间。这通常不会计算字符串中的最后一个单词。关于如何修复我的代码的任何建议。谢谢

    static void Main()
    {
        bool fileExists = false;

        string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string file = filePath + @"\wordcount.txt";

        fileExists = File.Exists(file);

        if (fileExists)
        {
            Console.WriteLine("{0} contains the following", file);
            Console.WriteLine(File.ReadAllLines(file));

            foreach (char words in file)
            {
                int stringCount = 0;
                if (words == ' ')
                {
                    stringCount++;
                }
                Console.WriteLine(stringCount);
            }

        }
        else
        {
            Console.WriteLine("The file does not exist, creating it");
            File.Create(file);
        }

        Console.ReadLine();
    }
我对它进行了编辑,以便检查内容而不是文件路径(noob在这里犯noob错误)。我仍然觉得我在foreach循环中使用if语句的逻辑很糟糕

        if (fileExists)
        {
            Console.WriteLine("{0} contains the following", file);
            string[] contents = File.ReadAllLines(file);

            foreach (string words in contents)
            {
                int stringCount = 0;
                if (words == " ")
                {
                    stringCount++;
                }
                Console.WriteLine(stringCount);
            }

        }
这是你应该看到的函数

var count = File.ReadAllText(file).Split(' ').Count();
这是你应该看到的函数

var count = File.ReadAllText(file).Split(' ').Count();

您不是在读取实际文件,而是在读取已声明为
filePath+@“\wordcount.txt”的
file
变量


您只是将文件内容输出到控制台。您应该将
File.ReadAllLines(File)
的结果分配给一个新变量(类型为
string[]
:),然后运行该变量。

您不是在读取实际文件,而是在读取声明为
filePath+@“\wordcount.txt”的
File
变量


您只是将文件内容输出到控制台。您应该将
File.ReadAllLines(File)
的结果分配给一个新变量(类型为
string[]
:),然后运行该变量。

如果您将文件内容读取到一个字符串,则可以使用此代码计算空格。您只需在该计数上加1即可处理最后一个单词

int count = strFileContents.Split(' ').Length - 1;

如果将文件内容读取为字符串,则可以使用此代码计算空格。您只需在该计数上加1即可处理最后一个单词

int count = strFileContents.Split(' ').Length - 1;

可以使用字符串拆分

 if (fileExists)
        {
            Console.WriteLine("{0} contains the following", file);
            Console.WriteLine(File.ReadAllLines(file));
            var fileContent=File.ReadAllText();

           stringCount=fileContent.Split(new [] {' ','\n'},StringSplitOptions.RemoveEmptyEntries).Length;
        }

可以使用字符串拆分

 if (fileExists)
        {
            Console.WriteLine("{0} contains the following", file);
            Console.WriteLine(File.ReadAllLines(file));
            var fileContent=File.ReadAllText();

           stringCount=fileContent.Split(new [] {' ','\n'},StringSplitOptions.RemoveEmptyEntries).Length;
        }
将文件读入字符串,按空格分割,计算数组中的项数


将文件读取为字符串,按空格分割,计算数组中的项目数。

我认为这应该符合您的格式要求:

List<string> allWords = new List<string>();
foreach (string words in file)
    {
      allWords += words;
    }

int wordCount = allWords.Length();
List allWords=new List();
foreach(文件中的字符串)
{
所有单词+=单词;
}
int wordCount=allWords.Length();

虽然,我认为@AlexeiLevenkov更好…

我认为这应该符合您的格式要求:

List<string> allWords = new List<string>();
foreach (string words in file)
    {
      allWords += words;
    }

int wordCount = allWords.Length();
Regex.Matches(File.ReadAllText(file), @"[\S]+").Count;
List allWords=new List();
foreach(文件中的字符串)
{
所有单词+=单词;
}
int wordCount=allWords.Length();

虽然,我认为@AlexeiLevenkov更好…

您应该阅读文档(还有示例代码)。您认为foreach反对文件路径“file”。这段代码有很多错误,我不知道从哪里开始!这与在foreach循环运行之前关闭文件有关吗?您应该阅读文档(其中也有示例代码)。您认为foreach反对文件路径“file”。这段代码有很多错误,我不知道从哪里开始!这与在foreach循环运行之前关闭文件有关吗?我将其与我已经编写的内容结合使用,这似乎起到了作用。谢谢。我把这个和我已经写过的东西结合起来使用,这似乎很管用。谢谢
Regex.Matches(File.ReadAllText(file), @"[\S]+").Count;