I often come across various postings in different forums asking for simple things like reading information from text file and parsing data from the text. Well, I get kind of tired of answering those question so I decided to write a blog about it and then point them to the blog. So here it is.
Now the text file named C:\Test.txt I have two simple line:
Firstname: Brian
Lastname: Dao
and I want to grab "Brian" & "Dao" out to use for some other purpose. The code is listed below:
Dim mySR As System.IO.StreamReader
Dim myFirstName, myLastName, myTempStr As String
mySR = System.IO.File.OpenText("C:\Test.txt")
If Not mySR.EndOfStream Then
myTempStr = mySR.ReadLine
myFirstName = Mid(myTempStr, InStr(myTempStr, "Firstname:", CompareMethod.Text) + 10)
myTempStr = mySR.ReadLine myLastName = Mid(myTempStr, InStr(myTempStr, "Lastname:", CompareMethod.Text) + 9)
End If
txtOutput.Text = myFirstName & vbCrLf & myLastName
mySR.Close()mySR = Nothing
Note that this doesn't take care of the leading space which could be trimmed by:
myTempStr.Trim
or
myTempStr = Trim(myTemStr)
Another way of accomplishing the same thing would be to call the Replace method and pass in the required parameters. The code sample below show two different ways you can call the Replace medhod.
myFirstName = Replace(myTempStr, "FirstName:", "", , , CompareMethod.Text)
myFirstName = myTempStr.Replace("FirstName:", "")
For those who are new to all this, be sure to look up the following string processing methods: InStr, InStrRev, Replace, Mid.
Showing posts with label Text. Show all posts
Showing posts with label Text. Show all posts
Wednesday, May 30, 2007
Thursday, May 24, 2007
Reading Text File Into Combo Box
In the previous blog (Reading Text File Using StreamReader Class), I demonstrated the use of StreadReader class to read text file. The System.IO.File class also provide us with a couple of ways to read text file as well.
We will use it's capability in this blog to read the data from text file and load it directly into a combo box.
Code sample:
string[] theStates;
theStates=System.IO.File.ReadAllLines(txtFilename.Text);
cboStates.Items.AddRange(theStates);
A sample application is available here.
We will use it's capability in this blog to read the data from text file and load it directly into a combo box.
Code sample:
string[] theStates;
theStates=System.IO.File.ReadAllLines(txtFilename.Text);
cboStates.Items.AddRange(theStates);
A sample application is available here.
Reading Text File Using StreamReader Class
The namespace System.IO has a class called StreamReader which can be used for reading text files.
The StreamReader class provides us with a couple of methods and a property as listed below:
To read a text file, we need to open it using System.IO.File.OpenText method which returns a StreamReader object. Once we obtain the StreamReader object, just call the appropriate method to pull the data.
C#
System.IO.StreamReader oSR;
oSR = System.IO.File.OpenText(sFilename);
txtOutput.Text = oSR.ReadToEnd();
VB
Dim oSR As System.IO.StreamReader
oSR = System.IO.File.OpenText(sFileName)
txtOutput.Text = oSR.ReadToEnd()
For further reading, please visit the this link.
Sample applications can be downloaded here: VB.net or C#
The StreamReader class provides us with a couple of methods and a property as listed below:
Close: Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.
Peek: Returns the next available character but does not consume it.
Read: Reads the next character or next set of characters from the input stream.
ReadLine: Reads a line of characters from the current stream and returns the data as a string.
ReadToEnd: Reads the stream from the current position to the end of the stream.
To read a text file, we need to open it using System.IO.File.OpenText method which returns a StreamReader object. Once we obtain the StreamReader object, just call the appropriate method to pull the data.
C#
System.IO.StreamReader oSR;
oSR = System.IO.File.OpenText(sFilename);
txtOutput.Text = oSR.ReadToEnd();
VB
Dim oSR As System.IO.StreamReader
oSR = System.IO.File.OpenText(sFileName)
txtOutput.Text = oSR.ReadToEnd()
For further reading, please visit the this link.
Sample applications can be downloaded here: VB.net or C#
Subscribe to:
Posts (Atom)
