Search code examples
c#textiotxt

how to find which line a specific text is located in a long string?


I have a string which consist of 60 lines. This string is located in a txt file and it is being edited continuously. So, the line that I am looking for always changes. How can I find the line that contains the following text: "/ 50"? And after I find that line, how can I create a new string that only has the text in that line?


Solution

  • Found the answer by myself. I am going to leave this here if anyone ever needs help on the same situation.

    int counter = 0;
    string line;
    
    // Read the file and display it line by line.
    System.IO.StreamReader file = new System.IO.StreamReader(dosyaYolu);
    while ((line = file.ReadLine()) != null)
    {
        if (line.Contains("/ 50"))
        {
            MessageBox.Show(counter.ToString() + ": " + line);
        }
    
        counter++;
    }
    
    file.Close();