473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

< Text - file to array >

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
4 9731
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gregor Horvath | last post by:
Hi, Before I reinvent the wheel I`d like to ask if someone has done this before since I did not find an advice at Google. The goal is to create a dynamic Tree View in HTML. Say I have a...
1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
4
by: fis | last post by:
Hi all, I've problem because there are needed break lines in my texts on the web site but i can't do it :( My pipeline looks like: XMS -> I18N -> XSLT -> HTML I have lot of texts in my...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
8
by: ASP Yaboh | last post by:
I have an ArrayList of data gathered from a database. I want to create a web page from this data by creating a <table>, each cell in each row displays the appropriate data. One of those cells in...
4
by: David Lozzi | last post by:
Howdy, I'm using a WYSIWYG editor called TinyMCE. When I edit some text and then save it back to my SQL server using a SQLCommand, all HTML characters are changed to HTML code, i.e. &gt;strong&lt;...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.