Alexandre Neves
Alexandre Neves

Reputation: 65

Need help reading Substrings in Files in C#?

So I created a subroutine to read some files... Here's the code:

dataGridView1.Rows.Clear();

            string[] lines = File.ReadAllLines(Application.StartupPath + listBox1.Text + ".txt");

            foreach (string line in lines)
        {

            int posicion1 = line.IndexOf("|") + 1;
            int posicion2 = line.IndexOf("|", posicion1) + 1;
            int posicion3 = line.IndexOf("|", posicion2) + 1;
            int posicion4 = line.IndexOf("|", posicion3) + 1;


            data1 = line.Substring(0, posicion1 - 1);
            data2 = line.Substring(posicion1, posicion2 - posicion1 - 1);
            formacao = line.Substring(posicion2, posicion3 - posicion2 - 1);
            relatorio = line.Substring(posicion3, posicion4 - posicion3 -1);

            dataGridView1.Rows.Add(data1, data2, formacao, relatorio);


        }

However, when it comes to adding the variable "relatorio" to the dataGridView, it just adds the ENTIRE line I have saved my variables on... Here's how I'm saving a line from my files for example:

28 de junho de 2017|28 de junho de 2017|fsdfsdfsd|Não

As you can see, I'm separating all of the variables with a "|". Summing Up, the program works correctly except the variable "relatorio" which is just adding the ENTIRE line (28 de junho de 2017|28 de junho de 2017|fsdfsdfsd|Não) when is just supposed to add "Não".

Can someone help me out? Sorry if the question is not clear enough.

-- UPDATE

So i have tried to use this instead:

 foreach (string line in lines)
        {

            string[] _line = line.Split('|');
            string value1 = _line[0];
            string value2 = _line[1];
            string value3 = _line[2];
            string value4 = _line[3];
            dataGridView1.Rows.Add(value1, value2, value3, value4);


        }

But STILL my program remains with the same mistake... I'm starting to think that I'm not correctly saving my files but i really don't see anywhere i screwed up... The file remains the same type when i close the program:

28 de junho de 2017|28 de junho de 2017|fwfew|Não

Can anyone test this out if you have the time of course?

Upvotes: 0

Views: 89

Answers (2)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131774

The String class already has a Split method:

var parts=line.Split('|');
dgv1.Rows.Add(parets[0], parts[1], parts[2], parts[3]);

The various Split overloads allow you to use multiple separators and eliminate empty results

You should probably look at parsers like CsvHelper. It can parse and map CSV data directly to objects, allow custom mappings and transformations, eg:

public class MyClass { public string Data1{get;set;} ...}

using(var reader=File.OpenText(csvPath))
{
    var csv = new CsvReader( textReader );
    csv.Configuration.Delimiter = "|";
    var records = csv.GetRecords<MyClass>().ToList();
    dgv1.DataSource=records;
}

Or use LINQ:

    var records = csv.GetRecords<MyClass>().Where(it=>it.Data1 >=...).ToList();

Upvotes: 2

Koby Douek
Koby Douek

Reputation: 16693

Using IndexOf, you are making things a bit more complicated than they should be. You can just use the Split method to get your values:

foreach (string line in lines)
{
     string[] _line = line.Split('|');
     string value1 = _line[0];
     string value2 = _line[1];
     ...

     dataGridView1.Rows.Add(value1 , value2 , ...);
}

Upvotes: 0

Related Questions