Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1349
  • Last Modified:

transform the CR/LF characters to \r\n

I have text data from FoxPro (memo).  There are a few paragraphs in a single transaction that I pull back into a WIN FORM dataset to present in a textbox.  No edit here, just read only.

What is the best way to repalce the CR/LF with the escape sequences that my WinForms will use?

Im pretty green at reqular expressions if that is the best way to handle this.

TIA



0
srussell705
Asked:
srussell705
  • 8
  • 4
  • 2
1 Solution
 
AvonWyssCommented:
CR==\r
LF==\n
Therefore, if the sequence is CRLF, you should not need any conversion.
0
 
srussell705Author Commented:
Thanks but trust me.  This is not the answer.

When I fill my textbox, I see two boxes butted next to one another for CR & LF in my display.  If I were to copy the text here it would break according to CR&LF.  

0
 
AvonWyssCommented:
Could it be that the data is LFCR instead of CRLF? Because if it IS CRLF, you'll not see boxes...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AvonWyssCommented:
You can try this:

string output=Regex.Replace(input, @"\r\n|\n\r|\r|\n", "\r\n", RegexOptions.SingleLine);
0
 
AgariciCommented:
i suggest replacing "\r\n" with Environment.NewLine

i guess your problem is because the textbox does not convert tex text corectly to unicode


hth.
A.
0
 
AgariciCommented:
btw you wont need regular expressions to do that
all you need is : YourTextBox.Text = YourTextBox.Text.Replace( "\r\n", Environment.NewLine );

hth.
A.
0
 
AvonWyssCommented:
Agarici, on Windows, Environment.Newline is always \r\n, so that you suggestion will just reproduce the string which was already present without changing anything.
0
 
srussell705Author Commented:

Thanks.  I was trying to figure out how to do the same but replace the \r\n with CHR(13) + CHR(10)

I'm playing with :
Char[] chars;
Byte[] bytes = new Byte[] {13,  10 };

ASCIIEncoding ascii = new ASCIIEncoding();


chars = ascii.GetChars(bytes,0,1) ;

But I'm not there yet.

any ideas?
0
 
AvonWyssCommented:
\r\n IS CHR(13) + CHR(10) - there is nothing to replace!
0
 
srussell705Author Commented:
Not getting funny now :(

my code:
                  Char[] chars;
                  Byte[] bytes = new Byte[] {13,  10 };

                  ASCIIEncoding ascii = new ASCIIEncoding();


                  chars = ascii.GetChars(bytes,0,2) ;//  " characters used to decode bytes." ;

                  int ln1 = 0;
                  string lcd ="";
                  foreach (Char c in chars)
                  {
                        lcd += chars[ln1] ;//Console.Write("[{0}]", c);
                        ln1 ++;
                  }
                  
                  mmTextBox2.Text = mmTextBox2.Text.Replace( lcd, Environment.NewLine );

lcd translates to "\r\n", and does no real work in the replacement as iI step through the code :(


0
 
AvonWyssCommented:
That's what I said, right?
0
 
AvonWyssCommented:
Did you even bother to see what line endings you get? And to try if the given regex works?
0
 
srussell705Author Commented:
I went back to the regEx example and bingo.  I wanted to work harder and not smarter I guess.

Thanks to all!!!
0
 
AvonWyssCommented:
Good to hear it worked for you. So there were other line breaks than \r\n in the string... ;-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now