Literal string problem

M

Mike

Hi.

I am trying to read a simle text file into a string variable, alter the text
using string.Replace(), then use the resulting text as the body for an
email.

This works fine up until the second step. When I read the text in from the
file, all the line breaks get replaced with '\n\r' characters. This would
be OK if, when I used the text as the email body, the converted back to
newlines, but this does not happen, so my email is on one long line filled
with \n's and \r's.

How do I "unescape" these characters to be actual newlines in my email?

Thanks in advance for your help,

Mike
 
M

Mike

Mike said:
Hi.

I am trying to read a simle text file into a string variable, alter the
text using string.Replace(), then use the resulting text as the body for
an email.

This works fine up until the second step. When I read the text in from
the file, all the line breaks get replaced with '\n\r' characters. This
would be OK if, when I used the text as the email body, the converted back
to newlines, but this does not happen, so my email is on one long line
filled with \n's and \r's.

How do I "unescape" these characters to be actual newlines in my email?

Thanks in advance for your help,

Just to clarify, after C# mangles the string, the email body looks like
this:

Fax \n\rNumber: 92630404\n\rThis email is a receipt of your recharge,
automatically sent to the email\r\naddress specified.\r\n\r\

I want it to look like this:

Fax
Number: 92630404
This email is a receipt of your recharge, automatically sent to the email
address specified.

It's driving me crazy. How do I do this?

Mike
 
M

Morten Wennevik

Hi Mike,

Could you provide a small but complete sample demonstrating your problem?
Do you manually add the breaks? They should be \r\n not \n\r
 
N

Niki Estner

Mike said:
Just to clarify, after C# mangles the string, the email body looks like
this:

Fax \n\rNumber: 92630404\n\rThis email is a receipt of your recharge,
automatically sent to the email\r\naddress specified.\r\n\r\

I want it to look like this:

Fax
Number: 92630404
This email is a receipt of your recharge, automatically sent to the email
address specified.

It's driving me crazy. How do I do this?

I suspect you look at that variable in the debugger?
C# usually doesn't "mangle" strings; However, the debugger (for several good
reasons) displays control-characters in a human-readable form. You could use
Console.WriteLine or Debug.WriteLine to check.

Since I don't know of any method that would replace control characters with
their proper escape codes, I assume that's what happening. If not, please
post a sample.

Niki
 
G

Guest

Mike,

Could you post the code which you are using to generate this output? I
assume you are not using an @ quoted string literal to insert these escapes?
(that symbol is used to prevent C# from processing the escape characters -
they are left in the string exactly as they are shown). If you are, simply
remove the @ from before the first quote.

Chris.
 
J

Jon Skeet [C# MVP]

Mike said:
Just to clarify, after C# mangles the string, the email body looks like
this:

Fax \n\rNumber: 92630404\n\rThis email is a receipt of your recharge,
automatically sent to the email\r\naddress specified.\r\n\r\

How are you viewing these strings? My guess is that you're seeing them
in the debugger, in which case no mangling has occurred at all - print
the string out to a console and you'll see it as you want to.
 
F

fd123456

Hi Mike,

You could just do this, assuming the text is in a variable called myText :

myText = myText.Replace("\n\r",Environement.NewLine);

HTH,

Michel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top