vbNewLine or VbCrLf Problem

usvpn

Freshman
Joined
Apr 19, 2010
Messages
45
Hi,
I have a text file on a remote server with this content:
-
Line1
Line2
Line3
...
-
I use this code to download it and put each line in a string (array)
Visual Basic:
Dim WebClient As New System.Net.WebClient
WebClient.Encoding = System.Text.Encoding.ASCII
WebClient.CachePolicy = New System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore)
Dim NewString As String = WebClient.DownloadString("http://www.domain.com/file.txt")
WebClient.Dispose()
Dim MyArray() As String = NewString.Split(vbNewLine)
For MyLoop As Integer = 0 To MyArray.Length - 1
    MsgBox(MyArray(MyLoop).ToString.Replace(vbNewLine, ""))
Next
It's OK, but when showing each line, the 1st line is OK.
After 2nd line, it applies a vbNewLine/vbCrLf to the beginning of each line.
And I CANNOT remove that, even with .Replace(vbNewLine, "")
How to remove this vbNewLine/vbCrLf from beginning of each line?
Thanks.
 
Edit: Nevermind. Read ATMA's post on the other forum.

It sounds like the best idea is to use the String.Trim function. You can specify a set of characters that you want trimmed, in this case carriage-return and line-feed.
Code:
    ' There are the line ending characters we don't want in our strings (LF and CR)
    Public Shared ReadOnly LineEndChars As Char() = {ChrW(10), ChrW(13)}

    ' This function removes line ending characters from a string
    Function Example(ByVal text As String) As String
        Return text.Trim(LineEndChars)
    End Function
 
Last edited:
Back
Top