Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get literal \r and \n when getting text from a database using C#?

Tags:

string

c#

In a database field, I'm storing and returning 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 (literal \r and \n):

TBD.\r\n\nExpect

Thoughts?

like image 848
Patrick From An IBank Avatar asked Aug 06 '10 10:08

Patrick From An IBank


2 Answers

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

like image 120
Noldorin Avatar answered Sep 23 '22 15:09

Noldorin


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 the system, editor, etc.

You could replace all occurrences of \\n\\r, etc. using:

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

This should work to fix your problem.

like image 27
Fish Avatar answered Sep 22 '22 15:09

Fish