Selecting a specific line from variable...

S

Sender

(Ignore this if this duplicate entry)

Scenario:

I have a notepad file c:\myfile.txt. There are 10 lines in that file with
text as follows:

This is my First line.
This is my Second line.
....................................
This is my Tenth line.

With the help of filestream, streamreader objects I could read the file. I
copied the contents of file in a string variable var1. Now I want to display
a specific line from the variable e.g. line no. 3 ("This is my third line").
What I did is (as suggested by some yesterday in this newsgroup) I created a
textbox1 and copied the contents of variable to this textbox1 and used the
textbox1.Lines(2) method to pick a specific line, and set the visible
property to false.

Is there any other way so that there is no need for textbox (though it is
hidden).

Thanks!
 
H

Herfried K. Wagner [MVP]

Sender said:
Scenario:

I have a notepad file c:\myfile.txt. There are 10 lines in that file with
text as follows:

This is my First line.
This is my Second line.
...................................
This is my Tenth line.

With the help of filestream, streamreader objects I could read the file. I
copied the contents of file in a string variable var1. Now I want to display
a specific line from the variable e.g. line no. 3 ("This is my third line").
What I did is (as suggested by some yesterday in this newsgroup) I created a
textbox1 and copied the contents of variable to this textbox1 and used the
textbox1.Lines(2) method to pick a specific line, and set the visible
property to false.

When using 'StreamReader', you can use its method 'ReadLine' to read a
whole line. You can add the lines read to an array list (class
'ArrayList'). Then you can access the lines by getting the appropriate
item of the array list.
 
R

Rafael Pivato

Use a more intelligent structure. Try putting the lines in an ArrayList (or
something) while reading from the file. So the index will give the line.



But, if you still wanting the String as structure try on of the following:



- Co-structure with indexes to the line breaks: you can create this with
an rrayList too, while reading the file.



- Use only CRLF or LF to accommodate line breaks at your string. By doing
this, you will be able to do a search for the substring (String.IndexOf).
 
C

Cor

Hi Sender,
This is my First line.
This is my Second line.
...................................
This is my Tenth line.

There are a lot of posibilities,
One other
\\\
dim mytext as string = split(textbox.text,vbcrlf)(2)
///
I hope this helps
Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top