문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top