C# text manipulation of crlf to cr

I need to have my string output with CR's only not CRLF's.  I am using c#.

Hello Experts,  Can one of you help a fox out?  I am reading in a text file (which is full of the CRLF's), I need to create a new file based on the text file and output the result with CR's only  I have tried a lot of methods that don't seem to work, here are my latest two failurs:

//richTextBox1.Text = HL7.Replace("\r\n", "\r");  //.Replace("\r\n", @" \r ");
                    richTextBox1.Text = HL7.Replace("\r\n", @" \r ");

I need a method that takes a string with crlf's and returns a string with cr's only

"help me obi-wan Kenobi, you are my only hope"

Thank you in advance,

Fox
LVL 9
the_b1ackfoxTechnophileAsked:
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.

NorieAnalyst Assistant Commented:
How are you 'reading' the file?
the_b1ackfoxTechnophileAuthor Commented:
StreamReader SR = new StreamReader(Target);
            while ((line = SR.ReadLine()) != null)
            {
                Ct++;
                Output += Pre + Ct.ToString() + Pre1 + line.Replace("\r\n", "") +Pre2 + "\r";
            }
            SR.Close();
AndyAinscowFreelance programmer / ConsultantCommented:
That should work.  (The removal of a CRLF and appending a CR at the end of your newly constructed line.)

How do you actually test it is not working?  Your code might function correctly - your testing procedure has the bug.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

the_b1ackfoxTechnophileAuthor Commented:
I'll copy output into notepad++ and view the outcome
AndyAinscowFreelance programmer / ConsultantCommented:
That is not a good way to test unless you can view the hex values in your notepad++.  Can you?
the_b1ackfoxTechnophileAuthor Commented:
I can see when the values are removed properly.
AndyAinscowFreelance programmer / ConsultantCommented:
OK, So you see it works in that case.

SNAG-0022.bmp

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
sarabandeCommented:
i suppose you read the text file in windows OS.

then, windows will replace CRLF by LF (or "\r\n" by "\n") automatically. you only can avoid that by reading the files in binary mode.

a (rich) edit box would add the CR (as a double-byte UTF16 character) again to the text.

so, the replacing works if it is made after reading the text formatted from edit box (or after manually adding the "\r\n" to the string) but not after reading from file. for the latter you may replace '\n' by '\r' although that sounds strange because i never encountered a string that contails CR's only.

Sara
the_b1ackfoxTechnophileAuthor Commented:
AndyAinscow & Sara,  Thank you both for showing me what I was missing.  Andy, you demonstrated I was on the right path, and Sara you added insight into why I was getting the result I was seeing.  I have thankfully learned something new because of your efforts.
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
C#

From novice to tech pro — start learning today.