Breaking a txt file into multiple items in a string list

1

I have a txt file with 2000 names. When I load in a list, the count is 1 and not 2000. Of course, since it is a text file it is only 1. It turns out that the file is organized with \n\r inside it. Even though I'm giving Split() , I still can not load a list with 2000 records or items. How do I do this? That is, get a txt file and divide it into a string list with multiple items, using the \n\r as separator?

I used this code to populate the array that comes from the txt file:

string[] text = new[] { System.IO.File.ReadAllText(path) };

I did it that way. I found it ugly, but I did not find a prettier solution, I had to make two foreach and that makes me kind of annoying.

string[] text = new[] { System.IO.File.ReadAllText(path) };

            foreach (var item in text)
            {
                string[] linha = item.Split('\n');
                foreach (var i in linha)
                {
                    lista.Add(i);
                }

            }
    
asked by anonymous 01.03.2016 / 17:21

3 answers

3

I could do this:

using System.Collections.Generic;
using System.IO;
using System.Linq;
List<string> texto = File.ReadAllLines(path).ToList();
    
02.03.2016 / 14:30
0

You can go through all the txt with your Giant String and feed the list to each unit found.

If the separator is \ n \ r, go through your String looking for this tab and use Substring to pass each of the items to the list.

    
01.03.2016 / 17:26
0
TextReader _Reader = new StreamReader(@"C:\ArquivoTexto.txt");

string[] _Splitado = _Reader.ReadToEnd().Split('|');

List<string> _Lista = new List<string>(_Splitado);

Does that solve it? Just change the file name and the character you want to use as a separator.

    
01.03.2016 / 21:50