在一行中检测单词的多个实例C#

时间:2017-01-07 14:20:53

标签: c# text console-application

在我建立的文件管理程序中,我有一种查找单词的方法。您输入了希望程序找到的单词。与TrueFalse一样。您还可以通过在程序中键入文件路径来为其提供文件路径。

public void searchForWord()
        {
            using (StreamReader fs = File.OpenText(filePath))
            {

                int count = 0; //counts the number of times wordResponse is found.
                int lineNumber = 0;
                while (!fs.EndOfStream)
                {
                    string line = fs.ReadLine();
                    lineNumber++;
                    int position = line.IndexOf(wordSearch);
                    if (position != -1)
                    {
                        count++;
                        Console.WriteLine("Match#{0} line {1}: {2}", count, lineNumber, wordSearch);
                    }
                }
                if (count == 0)
                {
                    Console.WriteLine("your word was not found!");
                }
                else
                {
                    Console.WriteLine("Your word " + "'" + wordSearch + "'" + " was found " + count + " times!");
                }
                Console.WriteLine("Press enter to quit.");
                Console.ReadKey();
            }
        }

该程序功能正常,但有一个错误,即使它起作用。这是我运行它的测试文件的片段。

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis lectus sem, eleifend in enim vel, imperdiet pretium justo. 
Vivamus sit amet arcu sed metus rutrum varius vitae vitae turpis. Quisque aliquet mauris lacus, quis aliquam metus placerat vel. 
Proin eleifend tortor velit, False quis aliquam arcu congue ut. Fusce sed mattis purus, sed vehicula diam. False Nullam in leo sit amet massa pharetra semper et vel diam. 
Sed at dolor aliquam libero bibendum porta.

如果您仔细阅读了我正在测试的段落的第三行,它有两组False,但我的程序只检测到第一行。

如下所示:

Match#1 line 3: False
Match#2 line 7: False
Match#3 line 17: False
Match#4 line 19: False
Match#5 line 24: False
Your word 'False' was found 5 times!

这是我的程序给我的输出。如何调整它以便在一行中考虑两个或多个单词的实例?

1 个答案:

答案 0 :(得分:0)

我使用indexOf找到的解决方案如下。

public void searchForWord()
        {
            using (StreamReader fs = File.OpenText(filePath))
            {

                int count = 0; //counts the number of times wordResponse is found.
                int lineNumber = 0;
                while (!fs.EndOfStream)
                {
                    string line = fs.ReadLine();
                    lineNumber++;
                    int position = line.IndexOf(wordSearch);
                    int i = 0;
                    while ((i = line.IndexOf(wordSearch, i)) != -1)
                    {
                        i++;
                        if (position != -1)
                        {
                            count++;
                            Console.WriteLine("Match#{0} line {1}: {2}", count, lineNumber, wordSearch);
                        }
                    }

                }
                if (count == 0)
                {
                    Console.WriteLine("your word was not found!");
                }
                else
                {
                    Console.WriteLine("Your word " + "'" + wordSearch + "'" + " was found " + count + " times!");
                }
                Console.WriteLine("Press enter to quit.");
                Console.ReadKey();
            }
        }