Home All Groups Group Topic Archive Search About
Author
2 Mar 2006 4:25 PM
gn
I am trying to parse a txt file which looks something like example
below:

thisem***@email.com
thatem***@email.com
anotherem***@email.com

This is the code I am using:

filetoread = file1.Value
filestream = File.OpenText(filetoread)
readcontents = filestream.ReadToEnd()
textdelimiter = " "
Dim splitout = Split(readcontents, textdelimiter)
Dim i As Integer
For i = 0 To UBound(splitout)
   lblsplittext.Text &= "<b>Split </b>" & i + 1 & ")   " & splitout(i)
& "<br>"
Next

This works fine if the file is in the format:

thisem***@email.com thatem***@email.com anotherem***@email.com

But what delimiter should I use to indicate a new line as a delimiter?

Thank you in advance...

Author
2 Mar 2006 4:48 PM
Virgil
You can use Environment.NewLine.
Author
2 Mar 2006 5:10 PM
gn
Thank you,

But how would I use that in the above code?
Author
2 Mar 2006 6:19 PM
Virgil
I made a small function to test the my answer before I sent it.  The
code is different (more DotNetish), but you are welcome to it =)

Import the following libraries at the top of your code:

Imports System.IO
Imports System.Text

Here was my method:

        Dim FileName As String = "test.txt"
        Dim FileContents As String
        Dim sr As StreamReader

        Try
            sr = File.OpenText(FileName)
            FileContents = sr.ReadToEnd()
        Catch ex As Exception
            MessageBox.Show("Unable top open file: " & ex.Message)
            Return
        Finally
            If (sr Is Nothing) = False Then
                sr.Close()
            End If
        End Try

        Dim Lines() As String = Split(FileContents,
Environment.NewLine)
        Dim Output As New StringBuilder
        Dim i As Integer
        For i = 0 To Lines.Length - 1
            Output.Append("<b>Split </b>" & i + 1 & ")   " & Lines(i) &
"<br />")
        Next

        lblSplitterText.Text = Output.ToString
Author
2 Mar 2006 8:49 PM
gn
Thank you so much - Perfect!