-3

I would like to store line by line from the text file after read it. However, the text that store into the temporary list must be after the ": ".

Below is the example of content in my text file:

Name: Johny
Age: 18
Favourite: Basketball, Food

I would like to store Johny as list[0], 18 as list[1], and etc. For the Favourite, it should be store separately such as Basketball as list[2] and Food as list[3] and etc. This is because I need to place it back to different textBox afterward.

Below is my example code:

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);
        }
    }
Llama
  • 25,925
  • 5
  • 49
  • 68
  • Use `File.ReadAllLines` to read all lines from a text file. Use `string.Split` to split strings into substrings at specific separator characters. – Clemens Dec 01 '20 at 09:42

2 Answers2

1

Here's a one liner with LINQ that reads the file into lines, split the lines on : and takes what's after, then splits that on , for further granularity:

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

Which outputs a list of: Johny, 18, Basketball, Food.

Haytam
  • 4,250
  • 2
  • 10
  • 37
0

You can:

  • read it line by line

  • separate the fields and it's valued by splitting it with : delimiter

  • check the line every three-line

  • split the favorite value by comma delimiter ,

  • clean the value with trim to remove whitespaces

      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);
          }
      }
    
Wildan Muhlis
  • 1,420
  • 1
  • 19
  • 40