Heyy... So, I have this .txt file with 5 lines, let's say:
I
Have
An
Easy
Question
If I wanted to add those lines to an array list position, how would I do that?
:confused:
Printable View
Heyy... So, I have this .txt file with 5 lines, let's say:
I
Have
An
Easy
Question
If I wanted to add those lines to an array list position, how would I do that?
:confused:
Instead of an array, use a List(of string) instead. It'll be much more flexible and user-friendly than an array will ever be (but you still have to learn arrays, which shows the basics of collecting things.)
All you really need to do is ADD them to the list, and viola, you're golden. :)
How could i add them? Using the streamreader.readline? or read to end?
As long as the function returns a string, you can add it to the list (as long as the list is set for strings.)
Here's an example:
Code:
Dim s As New StreamReader
Dim L As New List(Of String)
L.Add(s.ReadLine)
I tried your code, but it only seems to add the first line ONLY...
If I chage it to: line.AddRange(reader.ReadToEnd), it adds all the lines in the first position of the list...
I fixed the code... This is it:
vb Code:
Using reader As New IO.StreamReader("C:\Array.txt") line.AddRange(Split(reader.ReadToEnd, vbNewLine)) End Using
Any recommendations or anything.. Please do :D
Try this:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fileContents As String = File.ReadAllText("C:\temp\test.txt") Dim splitter() As Char = {Chr(13)} Dim myArray() As String = fileContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries) 'we have everything in the myArray array now 'lets fill it in a listbox for example ListBox1.DataSource = myArray End Sub
Pradeep :)
vb.net Code:
Dim lines As String() = IO.File.ReadAllLines("file path here")
Hi Pradeep..
Wont this work ?
vb Code:
Dim myArray() As String = System.IO.File.ReadAllLines("C:\temp\test.txt") 'we have everything in the myArray array now 'lets fill it in a listbox for example ListBox1.DataSource = myArray
That's much better and easier. :thumb:
Which one?
This. It is simple one line code that would get all the lines in the file in an array.vb Code:
Dim myArray() As String = System.IO.File.ReadAllLines("C:\temp\test.txt")
My code in post #7 was reading the file, then splitting it into an array. The only additional thing it was doing is removing any blank lines, if any. So that depends on which one you want to use.
Oh, I thought that what jmcilhinney wrote was a correction to your code. But the question remains... What's the basic difference between a list and an array?
An array is a basic structure (usually found in all languages), which provides a mechanism to address a (consecutive) sequence of memory locations by its index value. All the elements in an array are of same data type i.e. they occupy same memory size. Usually accessing an element is very fast since the memory blocks are of equal size. So we just multiply the index value with memory size and we get the address of the element. However, adding/inserting/removing elements is a cumbersome operation.
A List is a specialized .NET class that can be used to store elements more or less like arrays. But it has many advanced features. You can access elements by their index values just like arrays. And adding/inserting/deleting elements is just a matter of a simple function call to the user, besides many more advanced features.
Think of an array as an egg carton. When you make an egg carton it is a specific size, i.e. it can hold a specific number of eggs, and that size doesn't change. Initially each cup in the egg carton is empty. You can put an egg in each cup and you can remove eggs from those cups but the carton never changes size.
A List is like a magic egg carton. When you create it it exists but it has no cups. When you add an egg to magic carton it grows automatically so that it has just one cup for that one egg. If you add another egg the magic carton grows again to have two cups. You can then insert another egg in between those two and the carton grows again. You can then remove one of the eggs and the carton shrinks automatically so it's just got two cups for the two remaining eggs. If clear out all the remaining eggs the magic carton will shrink down again so it has no cups at all.
Arrays are very efficient so they should be used where you don't need this dynamic resizing behaviour. Lists are more convenient than arrays when dynamic resizing is required and, if a lot of resizing is required, then a List becomes more efficient than an array. That's because to "resize" an array you actually have to create a new array and copy all the elements from the original.
Here's something that not everyone knows: internally, a List actually stores its items in an array. The List provides fancy logic to hide that fact from the user but it's not really magic. ;) Each time you add an item to a List it first checks whether the array is large enough to hold it. If it's not it will resize the array as mentioned before. The thing is, the array doesn't get resized every time you add an item. When the array is resized its size is doubled. That keeps the number of copying operations to a balanced level and, the bigger the array gets, the less often it gets resized.
Oh, thanks a lot for that explanation. I liked the (magic) egg carton example.
But, just one last question:
Where would I use the dynamic resizing option(?) of the Lists?
Run this code:Code:Dim eggCarton As New List(Of String)
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.Add("Egg 1")
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.Add("Egg 2")
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.Insert(1, "Egg 3")
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.RemoveAt(0)
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.Clear()
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
Chr(13) I assume is for the enter button.
Splitting on Chr(13), which is a carriage return, is not such a good idea because, by default, a Windows text file should use a carriage return and line feed pair as a line break. If you split on just the carriage return then your substrings will all have leading line feeds.
In other words....?
This:
would become this:Code:1
2
3
4
5
Understand now?Code:12345
EDIT: wait....that's not right..thats for replacing..oops.
EDIT2: If I understand right, wouldn't it just add an extra carriage return between each line?
If your file uses carriage returns only as line breaks then you should split on ControlChars.Cr. If your file uses carriage return and line feed pairs as line breaks then you should split on ControlChars.NewLine or Environment.NewLine. I've never tried it but you may be able to handle both with the one line of code by using a String array as delimiters and specifying both, with NewLine first so that it gets preference. It might be worth testing that to see if it works, if you need it.
What exactly is a carriage? What's it function?
The names hearken back to the days of typewriters. The carriage was the apparatus that held the paper and it would physically move to the left as you typed so that the characters would appear left-to-right. At the end of a line you would want the carriage to return to its original position. The line feed occurred by turning the roller that held the paper within the carriage so that the paper was feed up the height of one line. Generally speaking, when you finished typing a line you would hit the handle on the left hand side and it would return the carriage to its original position and feed the paper one line, hence the names carriage return and line feed.
In Windows a line break in a text file is represent by the ASCII characters for a carriage return (13) and a line feed (10). In C# and other C-based languages these are represented with "\r" and "\n". In VB they can be represent individually with ControlChars.Cr and ControlChars.Lf or they can be represented together with ControlChars.CrLf or ControlChars.NewLine. Environment.NewLine will return a string containing a carriage return and a line feed on Windows systems, but on other systems it will return only a line feed because non-Windows systems use only a line feed to represent a line break.
Note that, these days, Windows will interpret a line feed on its own as a line break. This is one reason that it's bad to split on just carriage returns. In that case it will leave behind line feeds and those will become line breaks preceding each substring.