By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

< Text - file to array >

P: n/a
Hello Newsgroup,

I think this is easy for you: I want to save the data line by line into an
string array.

eg.

Text file: Array

Line 1 [0] Line1
Line 2 [1] Line 2
Line 3 [2] Line 3
Line n [3] Line n

How can I do this?
Thankx ...
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Carsten Kraft <Ca***********@RR-Software.de> wrote:
I think this is easy for you: I want to save the data line by line into an
string array.

eg.

Text file: Array

Line 1 [0] Line1
Line 2 [1] Line 2
Line 3 [2] Line 3
Line n [3] Line n

How can I do this?


Create an ArrayList.
Open the file with a StreamReader.
Repeatedly call ReadLine, saving the result to the ArrayList until the
result is null.
If you really need an ArrayList rather than an array, use

string[] array = (string[]) arrayList.ToArray(typeof(string));

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
Slurp the whole file, and split it! Be careful if your file is huge
though :)

public static string[] convertLinesIntoArray(string filename) {
TextReader textReader =
new StreamReader(new FileStream(filename, FileMode.Open));
Regex splitter = new Regex(Environment.NewLine);
return splitter.Split(textReader.ReadToEnd());
}

Carsten Kraft wrote:
Hello Newsgroup,

I think this is easy for you: I want to save the data line by line into an
string array.

eg.

Text file: Array

Line 1 [0] Line1
Line 2 [1] Line 2
Line 3 [2] Line 3
Line n [3] Line n

How can I do this?
Thankx ...

Nov 15 '05 #3

P: n/a
Why not just open a StreamReader, read all the contents of the file, and
split on \n.. That'll return a string array, seperated at linebreaks..

/Brian

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Carsten Kraft <Ca***********@RR-Software.de> wrote:
I think this is easy for you: I want to save the data line by line into an string array.

eg.

Text file: Array

Line 1 [0] Line1
Line 2 [1] Line 2
Line 3 [2] Line 3
Line n [3] Line n

How can I do this?


Create an ArrayList.
Open the file with a StreamReader.
Repeatedly call ReadLine, saving the result to the ArrayList until the
result is null.
If you really need an ArrayList rather than an array, use

string[] array = (string[]) arrayList.ToArray(typeof(string));

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4

P: n/a
Brian Hjøllund <br****@jubii.dk> wrote:
Why not just open a StreamReader, read all the contents of the file, and
split on \n.. That'll return a string array, seperated at linebreaks..


That would indeed do it - but you'd also want to get rid of all
occurrences of "\r", probably.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.