Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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_Io_Console - Fatal编程技术网

如何在文本文件中显示字符串?(C#控制台应用程序)

如何在文本文件中显示字符串?(C#控制台应用程序),c#,regex,io,console,C#,Regex,Io,Console,因此,我发现了如何显示输入字符串的出现次数,但我不知道如何显示单词的外观以及单词所在的句子。例如,我的输入字符串是“the”,但如何在控制台上将其显示为the或the或withered?另外,我如何显示这个输入字符串及其所在的句子?例如:“the”,一句话:干旱使公共汽车枯萎了 以下是我目前掌握的代码: static void Main(string[] args) { string line; int counter = 0; Cons

因此,我发现了如何显示输入字符串的出现次数,但我不知道如何显示单词的外观以及单词所在的句子。例如,我的输入字符串是“the”,但如何在控制台上将其显示为the或the或withered?另外,我如何显示这个输入字符串及其所在的句子?例如:“the”,一句话:干旱使公共汽车枯萎了

以下是我目前掌握的代码:

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++;
            if (line.IndexOf(userText, StringComparison.CurrentCultureIgnoreCase) != -1)
            {
                Console.WriteLine("Found on line number: {0}", counter);
                found++;
            }
        }
        Console.WriteLine("A total of {0} occurences found", found);
    }

换一行怎么样:

Console.WriteLine("Line {0}: {1}", counter,line);

顺便说一句,我不太明白你的问题,“在控制台上显示它作为或或枯萎”和“用它所在的句子显示这个输入字符串”是什么意思?

听起来你想要触发匹配的单词,即使它是匹配的部分单词

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 line number: {0}", counter, sWordFound);
        found++;
    }
}
这可能有错误,但应该把你推向正确的方向。此外,如果您在示例中包含预期的输出,这将很有帮助

这就是我对这段代码的期望:

input:
The drought withered the bus.
Luke's father is well known.
The THE ThE

output:
Found The on line number: 1
Found withered on line number: 1
Found the on line number: 1
Found father on line number: 2
Found The on line number: 3
Found THE on line number: 3
Found ThE on line number: 3

添加另一个Console.WriteLine以显示IF条件中的单词和句子。您好,因此在使用代码后,字符串子行代码中出现错误,因为它被使用了两次。第二个字符串子行是另一个字符串吗?我把它改名为string subline2,它就可以工作了。非常感谢您的帮助另一个问题,我如何使控制台输出包含“the”的实际单词。例如,它就像在葛底斯堡的第1行找到“THE”。在第4Glad号线上找到了父亲中的“父亲”,它可以为你工作。是的,不应该申报两次。重命名它是一个有效的解决方案。我想重用这个变量。在上面进行了更正。sWordFound应该包含find这个词(使用“fathers”行搜索应该返回“fathers”)。我可能没有writeline的正确语法。很抱歉,我应该更好地澄清输出。它应该显示“父亲”一词,然后显示句子。例如:在以下句子中找到“父亲”:父亲在这一行的编号上出现:4因为我的代码中的字符串=行代表整个句子,是否可以使sFoundWord只需要有一个包含“the”的单数单词?