Removing Carriage Returns in a line of text. Visual Basic .NET. VB .NET

Hello all,

I need a function which:

Finds and removes any instance of a carriage return in a line of text.

I am using a System.IO.StreamReader to read the data.


Dim dataFilepath as String = "C:\data\input.txt"
Dim fileDataIn As System.IO.File
Dim oRead As System.IO.StreamReader

oRead = fileDataIn.OpenText(dataFilepath)

Dim readData as String = oRead.ReadLine


Now, I need to search the String 'readData', and remove any instances of a carriage return.

I've tried the old VB Replace() function with no luck.

Thanks much,


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
If you're using readline (i..e Dim readData as String = oRead.ReadLine), won't the carriage return denote the end of the line (and not be read?)

If you're trying to take your entire file and add it to a string, without carraige returns, then...
1. Use the fileInfo object to find out how big your text file is.
2. Declare a stringbuilder (system.text.stringbuilder), using the file size to create it at the right size (i.e. new stringbuilder(filesize)).
3. Loop through the file until the readline returns nothing (or whatever failure is... don't recall offhand).
4. For each readline, use stringbuilder.append to append the line to the stringbuilder. No carriage returns will be added, since they denote the end of the line.
5. After all lines are read, use stringbuilder.tostring to get your string with all concatenations performed.
WhahAuthor Commented:

"If you're trying to take your entire file and add it to a string, without carraige returns, then..."

I'm merely trying to traverse a data file. It's actually a very simple issue that I haven't been able to solve.

The ReadLine() function is having trouble moving to the next line with the carriage return.

The first time ReadLine() is called, it gives me the line just fine. However, the next time it's called, it returns "".

Perhaps me using a StreamReader is the wrong approach?

I know that the carriage return is the issue, because after dtox'ing the file in UNIX, ReadLine() was able to process it just fine. But I'd rather not make the user call dtox everytime he or she wanted to use the app.

Jeff CertainCommented:
I'm doing something similar in one of my apps using TextReader -- it seems to handle carriage returns just fine

Dim input as System.IO.TextRead = File.OpenText(fileName)
Dim line as String
line = .ReadLine()
While Not (line Is Nothing)
  'Do something
  ' Get a new line
  line = .ReadLine()
End While
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

WhahAuthor Commented:

Thanks for your input.

I just attempted again with 'TextReader', and came up with the same results.

I also attempted to use Replace() and InStr() with the TextReader, to no avail.


Jeff CertainCommented:
Would you be willing to e-mail the function and the text file to me, so I can try to see what's going on for myself?
WhahAuthor Commented:

Jeff CertainCommented:
Could it be the fact that you've got empty lines between your text in which case you'll probably have to read the whole file into a string, replace all instances where you have 2 crlf with a single crlf & then load the modified string into your StreamReader.

Bob LearnedCommented:
Dim strNoCarriageReturn As String = strInput.Replace(ControlChars.Cr, String.Empty)


Actually ReadLine() returns string without CrLf.

you can try as follows, to see how replace works.
Dim dataFilepath as String = "C:\data\input.txt"
        Dim fileDataIn As System.IO.File
        Dim oRead As  System.IO.StreamReader

        oRead = fileDataIn.OpenText(dataFilepath)

        Dim readData(500) as Char
        dim strData as new String(readdata)

best of luck
how about do a read to end of file then use the replace function to get rid of the carriage returns.

Dim sreader As  System.IO.StreamReader
dim strData as String

strData = sreader.readtoend

strdata.replace(controlchars.CrLf, "<P>")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WhahAuthor Commented:
Thank you - I have distributed the points accordingly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.