从字符串后面的大文本文件中获取所有行

时间:2023-02-05 20:30:51

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.

我有很多巨大的文本文件,我需要在使用c#,fyi之后的某些字符串后检索所有行,该字符串将在最后几行内存在,但不确定最后有多少行。

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

2 个解决方案

#1


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

}

#2


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

#1


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

}

#2


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