如何在特定文本后逐行将文本文件存储到临时列表<string>

How to store text file line by line into temporary list<string> after specific text

我想在读取文本文件后逐行存储。但是,存储到临时列表中的文本必须在“:”之后。

下面是我的文本文件中的内容示例:

Name: Johny
Age: 18
Favourite: Basketball, Food

我想将 Johny 存储为列表[0],将 18 存储为列表[1],等等。 对于收藏夹,应该分开存储,例如篮球作为列表[2],食物作为列表[3]等。这是因为我需要在之后将它放回不同的文本框。

下面是我的示例代码:

private void storeDataList()
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.DefaultExt = ".txt";
        ofd.Filter = "Text Document (*.txt) | *.txt";
        string filename = ofd.FileName;
        string line = "";
        if (ofd.ShowDialog() == true)
        {
            StreamReader sr = new StreamReader(ofd.FileName);
            while (line != null)
            {
                for (int i = 0; i < 10; i++)
                {
                    List<string> elements = new List<string>();
                    string readText = File.ReadAllText(filename);
                    i = readText.LastIndexOf(": ");
                    elements.Add[i];
                }
            }
            sr.Close();
            detailsTextBox.Text = File.ReadAllText(filename);
        }
    }

这是一个带有 LINQ 的单行代码,它将文件读入行,在 : 上拆分行并获取后面的内容,然后在 , 上拆分以进一步细化:

var output = File.ReadAllLines(filename).SelectMany(l => l.Split(':')[1].Split(',').Select(s => s.Trim());

输出列表:Johny, 18, Basketball, Food.

您可以:

  • 逐行阅读

  • 分隔字段,用:定界符

    分割
  • 每三行检查一次

  • 用逗号分隔符分割喜欢的值,

  • 使用 trim 清除值以删除空格

      private void storeDataList()
      {
          OpenFileDialog ofd = new OpenFileDialog();
          ofd.DefaultExt = ".txt";
          ofd.Filter = "Text Document (*.txt) | *.txt";
          string filename = ofd.FileName;
          string line = "";
          if (ofd.ShowDialog() == true)
          {
              List<string> elements = new List<string>();
    
              using (var reader = new StreamReader(ofd.FileName))
              {
                  int rowNumber = 1;
    
                  //Dynamically stop the loop until the end of the stream
                  while (!reader.EndOfStream)
                  {
                      //Split to separate field name and values
                      var textLine = reader.ReadLine().Split(':');
    
                      //Since favorite fields are in multiples of three, we re-split the value by comma every three line
                      if (rowNumber % 3 == 0)
                      {
                          var favouriteArr = textLine[1].Split(',');
    
                          for (int i = 0; i < favouriteArr.Length; i++)
                          {
                              //trim to clean whitespace
                              elements.Add(favouriteArr[i].Trim());
                          }
                      }
                      else
                      {
                          elements.Add(textLine[1].Trim());
                      }
                      rowNumber++;
                  }
              }
    
              // # just a new delimiter to print the elements in a text box
              detailsTextBox.Text = string.Join('#', elements);
          }
      }