-3

I have a lot of huge text files, I need to retrive all lines after certain string using c#, fyi, the string will be there within last few lines, but not sure last how many lines.

sample text would be

someline
someline
someline
someline
etc
etc
"uniqueString"
line 1
line 2
line 3

I need to get lines

line 1
line 2
line 3
Rashed
  • 11
  • 5

2 Answers2

0

Try this code

    public string[] GetLines()
    {
        List<string> lines = new List<string>();
        bool startRead = false;
        string uniqueString = "uniqueString";
        using (StreamReader st = new StreamReader("File.txt"))
        {
            while (!st.EndOfStream)
            {
                if (!startRead && st.ReadLine().Equals(uniqueString))
                    startRead = true;
                if (!startRead)
                    continue;

                lines.Add(st.ReadLine());
            }
        }

        return lines.ToArray();
    }
yazan
  • 600
  • 7
  • 12
0
bool found=false;
List<String> lines = new List<String>();
foreach(var line in File.ReadLines(@"C:\MyFile.txt"))
{

 if(found)
 {
   lines.Add(line);
 }
 if(!found && line.Contains("UNIQUEstring"))
 {
   found=true;
 }

}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67