Question

I have a text file with 7000+ lines in it(in list form). I need to create an array that changes each line into a new index so that I can add checkboxes next to each item. (Which will then print to a .txt file if checked.)

I have found a few articles about how to do this but none of them really answered my question. I honestly don't even know where to begin turning it into an array. Please let me know what information you need to assist me.

Was it helpful?

Solution

If you need to create a List object by reading a file and want to have each separate line as a list item, here is what you can do.

//File name
const string f = "TextFile1.txt";

// Declare new List.
List<string> lines = new List<string>();

// Use using StreamReader for disposing.
using (StreamReader r = new StreamReader(f))
{
    string line;
    while ((line = r.ReadLine()) != null)
    {
    lines.Add(line);
    }
}

And then if you wish to convert this list into an array, you can use the .ToArray() method of the List object.

    string[] arrayOfLines = lines.ToArray();

I hope i got your question correctly and this helps. Thanks.

OTHER TIPS

string filename = @"C:\Example\existingfile.txt";
var lines = new List<string>();

using (StreamReader reader = File.OpenText(filename))
{
    while(!reader.EndOfStream) lines.Add(reader.ReadLine());
}

You can then iterate over the List like an array.

If your List is stored in a String you could try it the following way:

String largeList; 
String nl = System.getProperty("line.separator");

String[] lines = largeList.split(nl);

Lines contains now every line accessible by lines[x] -> x is the row number

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