我需要帮助从C#中的文本文件中显示单词(字符串)

时间:2015-12-01 05:10:54

标签: c# regex console

我需要帮助才能在控制台应用程序中显示文本文件中的单词。例如,我的输入字符串将是“the”,代码将通过文本文件读取并输出包含“the”的单词,例如“The”和“father”。我已准备好代码但输出整个句子包括单词而不是单词本身。代码如下所示:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace QuizTakeHome
{
    class Program
    {
        static void Main(string[] args)
        {
            string line;
            int counter = 0;

            Console.WriteLine("Enter a word to search for: ");
            string userText = Console.ReadLine();

            string file = "Gettysburg.txt";
            StreamReader myFile = new StreamReader(file);

            int found = 0;

            while ((line = myFile.ReadLine()) != null)
            {
                counter++;
                int index = line.IndexOf(userText, StringComparison.CurrentCultureIgnoreCase);
                if (index != -1)
                {
                    //Since we want the word that this entry is, we need to find the space in front of this word
                    string sWordFound = string.Empty;
                    string subLine = line.Substring(0, index);
                    int iWordStart = subLine.LastIndexOf(' ');
                    if (iWordStart == -1)
                    {
                        //If there is no space in front of this word, then this entry begins at the start of the line
                        iWordStart = 0;
                    }

                    //We also need to find the space after this word
                    subLine = line.Substring(index);
                    int iTempIndex = subLine.LastIndexOf(' ');
                    int iWordLength = -1;
                    if (iTempIndex == -1)
                    {    //If there is no space after this word, then this entry goes to the end of the line.
                        sWordFound = line.Substring(iWordStart);
                    }
                    else
                    {
                        iWordLength = iTempIndex + index - iWordStart;
                        sWordFound = line.Substring(iWordStart, iWordLength);

                    }

                    Console.WriteLine("Found {1} on the sentence: {1} on line number: {0}", counter, sWordFound, line);
                    found++;
                }
            }
            Console.WriteLine("A total of {0} occurences found", found);
        }
    }
   }

输出如下:

Output Snapshot

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

您可以从句子中创建令牌并检查每个令牌:

found = 0;
String[] tokens = line.Split(new char[] {' '});
foreach (String token in tokens) {
    if (token.IndexOf(userText, StringComparison.OrdinalIgnoreCase) != -1) {
        Console.WriteLine(token); // Do your stuff here
        found++; //increment to know how many times you found the word in the current line
    }
}
counter += found; //counter will contains all occurences in lines

此代码段使用您的代码(变量)。 要创建我们的令牌,我们必须拆分当前行,为此我们使用String.Split

我认为这是没有正则表达式功能的最好方法。 我希望它可以帮到你。

答案 1 :(得分:1)

您的Console.WriteLine错了。

使用此:

Console.WriteLine("Found {1} on the sentence: {2} on line number: {0}", counter, userText, sWordFound);

答案 2 :(得分:0)

这是执行此操作的代码。请记住,还有很多其他方法。

    var myfilter = "the";
    var lines = File.ReadAllLines(@"C:\myfile.txt");
    var wordsCsv = lines.Select(l => l.Split(new[] { ' ' }).Where(w => w.ToLower().Contains(myfilter.ToLower()))).Where(w => w.Any()).Aggregate("", (current, wordsList) => current + "," + string.Join(",", wordsList));