开发者

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

开发者 https://www.devze.com 2023-01-10 06:42 出处:网络
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:

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 开发者_如何学Cand \n):

TBD.\r\n\nExpect

Thoughts?


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


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.


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


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

text.Replace("\r\n",  System.Environment.NewLine);
0

精彩评论

暂无评论...
验证码 换一张
取 消