Link to home
Start Free TrialLog in
Avatar of Whah
Whah

asked on

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.

Ex:

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,

Whah!

SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Whah
Whah

ASKER

Hi,

"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.

Whah.
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
input.Close()
Avatar of Whah

ASKER

Chaosian,

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.

Whah.

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?
Avatar of Whah

ASKER

Sure.

Whah.
jcertain@edmlink.com
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.



SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Friend,

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
        oRead.ReadBlock(readdata,0,500)
        dim strData as new String(readdata)
        strData=strData.Replace(ControlChars.CrLf,"<P>")

best of luck
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Whah

ASKER

Thank you - I have distributed the points accordingly.