ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   CR LF problem (https://www.excelbanter.com/excel-programming/450758-cr-lf-problem.html)

Robert Crandal[_3_]

CR LF problem
 
I am using a user defined function, ReadTextFile(), to read
the contents of a text file into a string variable. The basic
code looks like this:

Dim s as string
s = ReadTextFile("C:\data.txt")

Now that the entire text file is stored in a string variable,
I would like to replace occurences of multiple carriage
returns (CR) or line feeds (LF) with a single carriage return.

In other words, I want all paragraphs to be single-spaced,
with only one CR and/or LF between paragraphs.

Finally, I need to write that single-space string to a new file,
and preserve the single-spaced format in the new file.

The biggest problem here, is that my text files come from
various sources, and each source uses different byte
representations of "carriage returns". I have noticed in some
files that a line break is represented by one CR (or
character 13 or hex "0D")....other files use CR-CR-LF
(or 13-13-10 or hex "0D-0D-0A") to represent a line break.
I think some files only use CR-LF ("0D-0A"), etc....

Does anyone know any good algorithms or functions that
can help replace any sequence of combinations of CR
and/or LF with just one CR LF line break?

Thank you

'-------------------------------------------------
' BTW, here is the ReadTextFile() function again:
Public Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()



Robert Crandal[_3_]

CR LF problem (BTW)
 
I forgot to mention..... I would like all line break characters
to be the same throughout the entire string. Using
CRLF or vbCrLf will be fine. Or, just one CR is good.

As long as it's consistent all throughout the string.




GS[_2_]

CR LF problem (BTW)
 
I forgot to mention..... I would like all line break characters
to be the same throughout the entire string. Using
CRLF or vbCrLf will be fine. Or, just one CR is good.

As long as it's consistent all throughout the string.


Ok, so if you say there are possibly 2 cr characters together, but only
1 LF character regardless of how many Cr chars, then...

'delete all CRs
s = Replace(s, vbCr, "")

'replace all LFs with CRLFs
s = Replace(s, vbLf, vbCrLf)

OR

s = Replace(Replace(s, vbCr, ""), vbLf, vbCrLf)

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




All times are GMT +1. The time now is 04:31 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com