Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-12-01
14
Medium Priority
?
1,035 Views
Last Modified: 2007-12-19
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!

0
Comment
Question by:Whah
12 Comments
 
LVL 24

Assisted Solution

by:Jeff Certain
Jeff Certain earned 330 total points
ID: 12721512
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.
0
 

Author Comment

by:Whah
ID: 12721636
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.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 12721678
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()
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Whah
ID: 12721843
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.

0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 12721859
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?
0
 

Author Comment

by:Whah
ID: 12721943
Sure.

Whah.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 12721947
jcertain@edmlink.com
0
 
LVL 10

Expert Comment

by:heintalus
ID: 12722021
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.



0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 330 total points
ID: 12722474
Dim strNoCarriageReturn As String = strInput.Replace(ControlChars.Cr, String.Empty)

Bob
0
 
LVL 1

Expert Comment

by:anupbhunia
ID: 12723342
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
0
 
LVL 9

Accepted Solution

by:
caball88 earned 390 total points
ID: 12724491
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>")
0
 

Author Comment

by:Whah
ID: 12978749
Thank you - I have distributed the points accordingly.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question