Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我需要帮助显示一个字(字符串)从一个文本文件在C#_C#_Regex_Console - Fatal编程技术网

我需要帮助显示一个字(字符串)从一个文本文件在C#

我需要帮助显示一个字(字符串)从一个文本文件在C#,c#,regex,console,C#,Regex,Console,我需要帮助在控制台应用程序中显示文本文件中的单词。例如,我的输入字符串是“the”,代码将读取文本文件并输出包含“the”的单词,例如“the”和“father”。我已经准备好了代码,但是它输出了整个句子,包括单词,而不是单词本身。代码如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using

我需要帮助在控制台应用程序中显示文本文件中的单词。例如,我的输入字符串是“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);
        }
    }
   }
输出如下所示:


有人能帮忙吗?

这里有一个代码。记住还有很多其他的方法

    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));

您可以从句子中创建标记并检查每个标记:

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
此代码段使用您的代码(变量)。 为了创建我们的代币,我们必须拆分当前行

我认为这是不使用正则表达式函数的最好方法。
我希望它能帮助您。

您的
控制台。WriteLine
错误

使用以下命令:


Console.WriteLine(“在句子:{2}上找到了{1},在行号:{0}上找到了,”,计数器,用户文本,sWordFound)

好吧,您可以逐行读取文件,然后在enire行中搜索匹配项。你应该一个字一个字地读文件,或者把行分开,然后做索引。你认为你能告诉我怎么做吗?好的,首先你需要确定“单词”的组成部分。单词可以用空格分隔,也可以用其他字符、comas、分号等分隔。我们在这里看到的是什么类型的输入?我的输入字符串是“the”。我已经准备好了一切,但我的剑犬正在输出“the”,但后面还有剩余的句子。我只需要剪掉“the”后面的部分。看看这个例子:嘿,你的代码工作了。但是我怎么计算我的输入字符串出现在代码中的次数呢?Nvm,我知道了。非常感谢你的代码。那我还需要剩下的代码吗?我不需要使用while循环,对吗?我编辑它是为了帮助你更多。您必须保留循环,但必须用代码段替换while循环中的代码。我希望它能帮助你。是的,我知道了。谢谢:)