Solved

transform the CR/LF characters to \r\n

Posted on 2004-09-14
14
1,339 Views
Last Modified: 2008-02-01
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
Comment
Question by:srussell705
  • 8
  • 4
  • 2
14 Comments
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12054647
CR==\r
LF==\n
Therefore, if the sequence is CRLF, you should not need any conversion.
0
 
LVL 1

Author Comment

by:srussell705
ID: 12055517
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12055532
Could it be that the data is LFCR instead of CRLF? Because if it IS CRLF, you'll not see boxes...
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12055552
You can try this:

string output=Regex.Replace(input, @"\r\n|\n\r|\r|\n", "\r\n", RegexOptions.SingleLine);
0
 
LVL 11

Expert Comment

by:Agarici
ID: 12056223
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
 
LVL 11

Expert Comment

by:Agarici
ID: 12056257
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12056726
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:srussell705
ID: 12057277

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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12057359
\r\n IS CHR(13) + CHR(10) - there is nothing to replace!
0
 
LVL 1

Author Comment

by:srussell705
ID: 12057408
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12057466
That's what I said, right?
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 200 total points
ID: 12057601
Did you even bother to see what line endings you get? And to try if the given regex works?
0
 
LVL 1

Author Comment

by:srussell705
ID: 12058485
I went back to the regEx example and bingo.  I wanted to work harder and not smarter I guess.

Thanks to all!!!
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12058541
Good to hear it worked for you. So there were other line breaks than \r\n in the string... ;-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is MicroStrategy.NET? 2 45
dispose of a DataTable created in another class 4 22
Data Saving 5 36
Long URL in SMS 6 24
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now