C# text manipulation of crlf to cr

the_b1ackfox
the_b1ackfox used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
How are you 'reading' the file?
the_b1ackfoxTechnophile

Author

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 / Consultant

Commented:
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.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

the_b1ackfoxTechnophile

Author

Commented:
I'll copy output into notepad++ and view the outcome
AndyAinscowFreelance programmer / Consultant

Commented:
That is not a good way to test unless you can view the hex values in your notepad++.  Can you?
the_b1ackfoxTechnophile

Author

Commented:
I can see when the values are removed properly.
Freelance programmer / Consultant
Commented:
OK, So you see it works in that case.

SNAG-0022.bmp
Top Expert 2016
Commented:
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_b1ackfoxTechnophile

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial