Question

I am reading in a text file and splitting it at delimited point. Everything before the : is the left value, and everything after is the right. What is the best loop through the lines and store the string values into 2 lists, a left and a right? I don't need them to output, just store the values in the list.

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        id newId = new id();
        newId.Left = "";
        id newId2 = new id();
        newId2.Right = "";

    }
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog of = new OpenFileDialog();
        of.ShowDialog();
        textBox1.Text = of.FileName;
        using (StreamReader sr = new StreamReader(of.FileName));

        string content = File.ReadAllText(of.FileName);
        string[] split = content.Split(';' , ':');
        foreach (string segment in split)
        {

            if 

        }

              }
Was it helpful?

Solution

This should work. Not sure if you wanted the semicolon at the end so I just removed it.

var left = new List<string>();
var right = new List<string>();
using (var diag = new OpenFileDialog()) {
    if (diag.ShowDialog() == DialogResult.OK) {
        using (var sr = new StreamReader(diag.FileName)) {
            string line;
            while ((line = sr.ReadLine()) != null) {
                var split = line.Replace(";", "").Split(':');
                if (split[0] != null && split[1] != null) {
                    left.Add(split[0]);
                    right.Add(split[1]);
                }
            }
        }
    }
}

I noticed you mentioned this is a key/value thing so I would actually do:

var dict = new Dictionary<string, string>();
using (var diag = new OpenFileDialog()) {
    if (diag.ShowDialog() == DialogResult.OK) {
        using (var sr = new StreamReader(diag.FileName)) {
            string line;
            while ((line = sr.ReadLine()) != null) {
                var split = line.Replace(";", "").Split(':');
                if (split[0] != null && split[1] != null)
                    dict.Add(split[0], split[1]);
            }
        }
    }
}

Could be cleaned up a bit but you see the point. You were reading the entire file and then splitting. How would you know which was on the left and right of each line? Maybe check even and odds? That's terrible though when you can just read a single line and then split that. Then just grab the first and second items in the array and you have your two items.

OTHER TIPS

For what you have provided, a foreach can do fine.

As for how to store them, that's entirely dependent on what you need to do with them. If the items on "left" are all unique, you can make a dictionary, where "left" is the key and "right" is the value. That would be easy to work with. Otherwise, if "left" is not unique, you can have a list of Tuples (List<Tuple<string,string>>) or KeyValuePairs (List<KeyValuePair<string,string>>).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top