views:

150

answers:

4

In a database field, I'm returning I'm storing the "body" of my email (in case it changes). In this I have \n\r characters to get new lines. But, it seems not to be working. Any thoughts:

So data in field: 'TBD.\r\n\nExpect'

And my output looks like that TBD.\r\n\nExpect

Thoughts?

+1  A: 

replace your \r\n with System.Environment.NewLine as below may do work for you

text.Replace("\r\n",  System.Environment.NewLine);
Pranay Rana
Only if the e-mail body is html formatted, not in plain text.
Mikael Svenson
The issue here isn't really to do with HTML, it's to do with the way strings are interpreted at compile-time and run-time.
Noldorin
answer is updated now
Pranay Rana
+9  A: 

Escape sequences have no meaning within actual string objects - only when the C# parser/compiler interprets them. You want to store an actual new line in your database field rather than the 4 characters "\r\n".

Noldorin
+1  A: 

Because \r \n etc. works only within a string in your c# code. If you read a string from a file, a db , or other things, they just get they verbatim values...

Andrea Parodi
+1 This is effectively the same as what I'm saying..
Noldorin
Yes. +1 to you too. Also, the <br/> tag works only if the email body is in html format.
Andrea Parodi
A: 

It is likely that the \r\n is escaped, so the string actually returned would be equivalent to a string

myString = "\\r\\n";

so you would need to remove these extra slashes either when adding or removing from the database.

Though likely unrelated to your problem, the extra \n you have may cause viewing problems depending on system and editor etc.

You could replace all occurrances of \\n\\r etc using:

replacedString = myString.Replace("\\r\\n", "\r\n");

This should work to fix your problem.

Fish