We help IT Professionals succeed at work.

C# text manipulation of crlf to cr

the_b1ackfox
the_b1ackfox asked
on
88 Views
Last Modified: 2019-01-25
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

NorieAnalyst Assistant
CERTIFIED EXPERT

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
CERTIFIED EXPERT

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.
the_b1ackfoxTechnophile

Author

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

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
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
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.