using (StreamReader readFile = new StreamReader(filepath))
            {
                string line;
                while ((line = readFile.ReadLine()) != null)
                {
                    textBox1.Text = textBox1.Text + line + Environment.NewLine;
                }
            }

This is what I have so far. I have tried using .Split to try to seperate them but do not know much about it.
So far of course all this does is show the what is in CSV to the textbox. What I am trying to do it ignore say the first three columns then read the next three then ignore the last three.

Was thinking of putting each row into an array then spliting it that way but can't figure out how to do that either.

Any help would be awesome.
Thanks

Recommended Answers

All 18 Replies

See the examples here how to split the string into it's individual tokens

See the examples here how to split the string into it's individual token

Everytime I try to use split I get

    System.String[]
    System.String[]
    System.String[]
    System.String[]
    System.String[]

here is what I am doing

string filepath = "C:/****";
                using (StreamReader readFile = new StreamReader(filepath))
                {
                    string line;
                    while ((line = readFile.ReadLine()) != null)
                    {
                        string[] split = line.Split(',');
                        textBox1.Text = textBox1.Text + split + Environment.NewLine;
                    }
                }

If you want each value to appear in the text box on it's own line, then below is one way to do it. I couldn't get it to work the way you tried it.

            using (StreamReader readFile = new StreamReader("C:..."))
            {
                string line;
                while ((line = readFile.ReadLine()) != null)
                {
                    string[] split = line.Split(',');
                    int count = 0;
                    foreach (string ln in split)
                    {
                        if( count++ > 2) // ignore first 3 columns
                            textBox1.Text += (ln + Environment.NewLine);
                    }
                }
            }

Loop the split array with a foreach and add it to the same string + Environment.NewLine and finally set the textBox1.Text property with the string.

Okay so got it working.

StreamReader sr = new StreamReader(filepath);
                string fileString = sr.ReadToEnd();
                string[] lines = fileString.Split('\n');
                string[] stringArray = lines[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in stringArray)
                {
                    textBox1.Text = textBox1.Text + s + Environment.NewLine;
                }

Now I need it to keep going through all the lines till hits the last one.
Trying to figure out how to iterate through the array till it hits the end of the CSV file. Adding 1 to lines[0] till the end.

The code I posted will do exactly that. Why are you using an array of lines?

if you want each value to appear in the text box on it's own line, then below is one way to do it. I couldn't get it to work the way you tried it.

            StreamReader sr = new StreamReader(filepath);
                string fileString = sr.ReadToEnd();
                string[] lines = fileString.Split('\n');
                string[] stringArray = lines[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                int count = 0;
                foreach (string s in stringArray)
                {
                    if (count++ > 4)
                        textBox1.Text += (s + Environment.NewLine);
                }

This is good. Thanks man just need to do iterations now through the array :)

If you don't care about which values are on which lines, then why not combine lines 3 and 4 so that it does split only once?

            StreamReader sr = new StreamReader(filepath);
                string fileString = sr.ReadToEnd();
                string[] stringArray = fileString.Split(new char[] {',','\n'});
                int count = 0;
                foreach (string s in stringArray)
                {
                    if (count++ > 4)
                        textBox1.Text += (s + Environment.NewLine);
                }

If you have a lot of items to display then use a listbox instead of a textbox so that you can see them all. textbox doesn't have a scrollbar.

Just a quick sketch:

const int EOF = -1;

using (StreamReader SR = new StreamReader(filePath))
{
    while (SR.Peek() != EOF) //I like EOF!
    {
        string Aline = SR.ReadLine();
        List<string> CVSValues = Aline.Split(',').ToList();
        // think you need CVSValues[3], CVSValues[4] and CVSValues[5]
          // etc.

Are you mixing C++ and C#? No need for that loop.

If you don't care about which values are on which lines, then why not combine lines 3 and 4 so that it does split only once?

Trying to get only the 4th-6th columns to show. Trying to do an array so I can set up up like "Here is "Name(Fourth Column)" and I want to eat "Pizza(5th Column" and I will be at "pizza hut(6th column)"

Then it goes to the next row
"Here is "Name(Fourth Column)" and I want to eat "goose(5th Column" and I will be at "My House(6th column)"

And so forth till the end of the file.
If that makes any sense >.<

By using ReadToEnd() you lose track of individual lines, so you'd have to know beforehand how many columns are on a line then extract them from the array as if everything was in one big line of the file. So you'd have to set it up something like this:

 const int ColumnsPerLine = 10;
 int count = 0;
 foreach (string s in stringArray)
 {
    ++count;
    if (count > 4 && count < 7)
       textBox1.Text += (s + Environment.NewLine);
    if( count >= ColumnsPerLine)
       count = 0; // reset for next line
 }

I would think it would be a lot more understandable if the file were just read one line at a time, like in your original post.

@AD Are you mixing C++ and C#? No need for that loop.

Sorry ad, although it may slightly resemble C++, this is real 100% C# code!
The ReadLine is done in the loop. If the while Peeks EOF it stops.

List<string> CVSValues

Looks like C++ to me, does C# have similar containers?

using (StreamReader readFile = new StreamReader(filepath))
            {
                string line;
                while ((line = readFile.ReadLine()) != null)
                {
                    string[] split = Regex.Split(line, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
                    int count = 0;
                    foreach (string ln in split)
                    {
                        if (count > 4 && count < 8)
                        {
                            textBox1.Text += (ln + Environment.NewLine);
                        }

                        count++;
                    }
                }
            }

Got this far. Does what I want. Displays only the 3 columns I want. Now I need to turn that say first row into a string. Then turn the next 3 columns in the next row into a string and so forth.

Sorry If that makes no sense.

Just replace textBox1.Text with string objects. Using ddanbe's suggestion

List<string> items;
                    foreach (string ln in split)
                    {
                        string n;
                        if (count > 4 && count < 8)
                        {
                            n += (ln + Environment.NewLine);
                        }
                        items.add(n);
                        count++;
                    }

C# has taken over many features of C++ and Java. It has been said that the # sign are in fact two + signs joined somewhat together.
C# has what is called generics, example

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.