0

私が取り組んでいる小さなコンソール アプリケーションがあり、実際の単語数ではなく、いくつかの 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);
            }

        }
4

7 に答える 7

3

String.SplitFile.ReadAllTextは、注目すべき関数です。

var count = File.ReadAllText(file).Split(' ').Count();
于 2013-08-07T19:43:05.553 に答える
3

実際のファイルを読んでいるのではなく、fileとして宣言した変数を読んでいますfilePath + @"\wordcount.txt";

ファイルの内容をコンソールに出力しているだけです。File.ReadAllLines(file)からの結果を新しい変数 (タイプstring[]: http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx )に割り当ててから、それを実行する必要があります。

于 2013-08-07T19:44:21.400 に答える
0

ファイルの内容を文字列に読み取る場合、このコードを使用してスペースをカウントできます。最後の単語を処理するには、そのカウントに 1 を追加するだけです。

int count = strFileContents.Split(' ').Length - 1;
于 2013-08-07T19:44:58.843 に答える
0

文字列分割を使用できます

 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;
        }
于 2013-08-07T19:45:09.100 に答える
0
if (fileExists)
    {
        string fileString = File.ReadAllText(file);
        var words = fileString.Split(' ');
        int strCount = words.Count();
    }

ファイルを文字列に読み取り、スペースで分割し、配列内のアイテムの数を数えます。

于 2013-08-07T19:45:18.040 に答える
0

これはあなたのフォーマットを満たすべきだと思います:

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

int wordCount = allWords.Length();

ただし、@AlexeiLevenkovの方が優れていると思います...

于 2013-08-07T19:46:29.193 に答える
0
Regex.Matches(File.ReadAllText(file), @"[\S]+").Count;
于 2013-08-07T19:46:38.287 に答える