-4

Let's say I have a string "Toast" and I want to check to see if line "n" of a text file contains that string. How do I go about this? I know "n"

GeassAye
  • 15
  • 1
  • Does this answer your question? [How do I read a specified line in a text file?](https://stackoverflow.com/questions/1262965/how-do-i-read-a-specified-line-in-a-text-file) – Eugene Podskal Nov 29 '19 at 22:33
  • Welcome to StackOverflow. Please read our [help]. There, you'll come to understand why you are getting downvoted as you did not supply [mvce] or show that you have made any effort of your own to solve this issue. StackOverflow is not a free coding service. We are here to help you *learn*. How can you *learn* if we just give you the answers? :) – Jaskier Nov 29 '19 at 22:33

1 Answers1

1

Simple solution:

        using(var reader = new StreamReader("MyFile.txt"))
        {
            string line;
            int lineNumber = 0;
            while((line = reader.ReadLine()) != null)
            {
                lineNumber++;

                if (line.Contains("Toast"))
                {
                    Console.WriteLine($"Found 'Toast' on line {lineNumber}");
                }
            }
        }
user2966445
  • 1,267
  • 16
  • 39
  • 1
    You can simplify the `while` statement by using `while (!reader.EndOfStream)`. Despite that- it's typically frowned upon to answer a question where hundreds of it identical have already been asked and marked as duplicates. This just shows OP doesn't have to do any work on their end in order to get the answer they want – Jaskier Nov 29 '19 at 22:30