使用关键字(已解决)从文本中搜索一行,并在搜索到的行之后显示第三行

时间:2019-07-09 14:45:39

标签: c#

我想用文本中的关键字搜索初始行后显示第三行:

,我想在文本框中第三行中列出所有变量。 关键字是[Ref 1]

The text file

        {  // string motcledm = "code:A14";
            string line;

            string motcletest = SEARCH.Text;

            using (System.IO.StreamReader file = new System.IO.StreamReader(@"D:\\TEST.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    if ((line.Contains(motcletest)))
                    {
                        richTextBox1.Text = line.ToString();
                    }
                }

我需要的输出 THE OUTPUT

2 个答案:

答案 0 :(得分:1)

如前所述,您已经提取了第三行,位于“”上的Split,以获得字符串数组

  String thirdLine = "F8,F9,...";
  String[] strArray = thirdLine.Split(',');

  foreach(string _val in strArray){
           //do your stuff
  }

答案 1 :(得分:1)

        string line;

        string motcletest = SEARCH.Text;

        using (System.IO.StreamReader file = new System.IO.StreamReader(@"D:\\TEST.txt"))
        {
            while ((line = file.ReadLine()) != null)
            {
                if ((line.Contains(motcletest)))
                {
                    richTextBox1.Text = line.ToString();
                    file.ReadLine();//read first line after matching line
                    file.ReadLine();//read second line after matching line
                    line = file.ReadLine(); //third line that you are looking for
                    foreach(var value in line.Split(','))//split by ,
                    {
                       //Add the value the controls(textbox)
                       //if the count is not fixed, you might need to create a control and add it to a panel
                    }
                }
            }