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);
}
}
You can:
read it line by line
separate the fields and it's valued by splitting it with
:
delimitercheck the line every three-line
split the favorite value by comma delimiter
,
clean the value with
trim
to remove whitespaces