Replace line feeds or carriage returns with a Space

Hi,

In VB is it possible to Replace line or carriage feeds with a space character?
For example if I have string that reads

Test Address
Address 1
Address 2
Address 3

I want to change it so it reads:

Test Address Address 1 Address 2 Address 3.

Any help much appreciated.
JakeyCakesAsked:
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.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
replace  ( replace ( replace ( variable vbcrlf , " " ), vblf , " " ), vbcr , " " )
herreruudCommented:
Hi - Two easy ways to do this

First, you can use the replace function.
Use one of the following

sTest = Replace(sTest, vbNewLine, " ")
sTest = Replace(sTest, vbCrLf, " ")
sTest = Replace(sTest, chr(13) & chr(10), " ")

Or, you could split the information into an array and join it back togeather again:

Dim sArr() as string
sArr = Split(sTest, vbNewLine)
sTest = Join(sArr, " ")

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
snakemasterCommented:
Carriage and lineFeeds are VB constants.
You can remove them with the following code:

Private Function RemoveCrAndLF(ByVal content As String) As String
    Dim result As String
   
    result = Replace(content, vbCrLf, " ")
    result = Replace(content, vbCr, " ")
    result = Replace(content, vbLf, " ")
   
    RemoveCrAndLF = result
End Function

I don't know which text editor you are using, but most likely you will see that they put a carriage return AND a line feed after a line. Therefore I first replace those. Any remaining carriage returns and Line feeds are then also replaced with a space.
JakeyCakesAuthor Commented:
Hi,

All three work and are excellent answers, so I'm going split the points evenly,

Thanks to all of you
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 Classic

From novice to tech pro — start learning today.