Search code examples
c#wpflistread-data

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


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

Solution

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