How to remove carriage returns

IneedaSmoke

Active member
Joined
Mar 4, 2008
Messages
26
Programming Experience
Beginner
Hey all. I'm having a bugger of a time trying to remove carriage returns. I have a large text file with a bunch of little squares. I'm guessing they're carriage returns from what i've googled. They are not standard ascii characters.

Anywho, i'de like to get rid of them. I've got a loop that tests this condition:

If textCharArray((charPosition) + (133 * rowPosition)) = Environment.NewLine Then
textCharArray((charPosition) + (133 * rowPosition)) = ""

End If

Where 133 is how long each row is if there isn't a carriage return and 134 if there is, char position = where in the row, and rowPosition is which row i'm looking at.

I've tried:

Environment. Newline
vbcrlf
vbcr
vblf
chr(10)
chr(13)

Does anyone have any suggestions? TIA
 
VB.NET:
        Dim sr As StreamReader = New StreamReader(sInputFile)
        Dim sw As StreamWriter = New StreamWriter(sOutputfile)
        sw.Write(sr.ReadToEnd().Replace(Environment.NewLine, String.Empty))
        sr.Close()
        sw.Close()

I'd get a text editor that will show you what the squares actually are. I use Notepad++ daily and you can view the characters via View --> Show all characters.
 
Last edited:
Thank you for responding. Notepad++ is awesome! I copied some data over there and it shows CR LF. I'm not sure why my previous attempts have failed so far. I tried to include the replace that you suggest but it did not work. I'm truly perplexed at this point.
 
It works! It's so beautiful it brings a tear to my eye :p

If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
'TextBox1.Text = objReader.ReadToEnd
textString = (objReader.ReadToEnd().Replace(vbLf, String.Empty))
objReader.Close()
Else
MsgBox("File Does Not Exist")
End If
textCharArray = textString.ToCharArray()

I used your code but with vbLf. When I copied over it gave me CrLf but when I opened up the original file it showed only Lf.

Interesting enough this code does not work:
If (textCharArray((charPosition) + (133 * rowPosition)) = vbCr Or _
textCharArray((charPosition) + (133 * rowPosition)) = vbLf Or _
textCharArray((charPosition) + (133 * rowPosition)) = vbCrLf) Then

textCharArray((charPosition) + (133 * rowPosition)) = ""

I haven't tried replacing the "" with no value though.

Anywho, huzzah! Main points I think are to use a good text editor such as notepad++ and to use replace. Thanks for the great help!
 
Back
Top