Link to home
Start Free TrialLog in
Avatar of the_b1ackfox
the_b1ackfoxFlag for United States of America

asked on

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
Avatar of Norie
Norie

How are you 'reading' the file?
Avatar of the_b1ackfox

ASKER

StreamReader SR = new StreamReader(Target);
            while ((line = SR.ReadLine()) != null)
            {
                Ct++;
                Output += Pre + Ct.ToString() + Pre1 + line.Replace("\r\n", "") +Pre2 + "\r";
            }
            SR.Close();
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.
I'll copy output into notepad++ and view the outcome
That is not a good way to test unless you can view the hex values in your notepad++.  Can you?
I can see when the values are removed properly.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.