Delimiter question

  • Thread starter Thread starter gn
  • Start date Start date
G

gn

I am trying to parse a txt file which looks something like example
below:

(e-mail address removed)
(e-mail address removed)
(e-mail address removed)

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:

(e-mail address removed) (e-mail address removed) (e-mail address removed)

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

Thank you in advance...
 
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
 
Back
Top