为什么在替换时,c# .net会删除我的换行符,我怎么才能停止它呢?

时间:2022-08-16 07:14:11

I have a textarea saving to a database that I'm using to send as the body of an email.

我有一个文本区域保存到一个数据库中,用来作为电子邮件的主体发送。

I allow tokens to be used as placeholders for information pertaining to that message.

我允许令牌用作与该消息相关的信息的占位符。

If I don't touch the placeholders at all the email sends just fine with the line breaks exactly as they are in the textbox (the email is being sent in plain text).

如果我在所有邮件中没有接触到占位符,那么就可以在换行符的时候正好与在文本框中一样(邮件以纯文本的形式发送)。

However, when I start using the replace function, the new line characters start disappearing and all the lines get pushed together.

但是,当我开始使用replace函数时,新的行字符开始消失,所有的行被挤在一起。

For example.

为例。

Body.Replace("%procedure%", CurrentOrder.Description);

Will replace the text %procedure%, but will also remove the newline at the end of the line. Even if the newline isn't directly after the text being replaced.

将替换文本%过程%,但也将删除行末尾的换行。即使在替换文本之后换行并不直接。

Any ideas?

什么好主意吗?

edit:

编辑:

For now, I'm just replacing "\n" with "<br />" and sending the email as HTML. I would rather keep it as plain text as I don't have control over the recipients at all.

目前,我只是将“\n”替换为“
”,并以HTML的形式发送电子邮件。我宁愿把它保持为纯文本,因为我根本无法控制收件人。

EDIT 2: It appears to be an issue with outlook itself, not the email. I just viewed the exact same email in gmail, and the format was correct.

编辑2:这似乎是outlook本身的问题,而不是邮件本身的问题。我只是在gmail中看到了完全相同的邮件,格式是正确的。

2 个解决方案

#1


3  

Outlook removes new lines unless the line ends with two spaces.

Outlook删除新的行,除非行以两个空格结束。

If you're testing the emails to an account that uses Outlook try adding two spaces before your new lines and see if that fixes it.

如果您正在测试一个使用Outlook的帐户的电子邮件,请尝试在新行之前添加两个空格,看看这是否能修复它。

#2


0  

If its for HTML I would first of all replace the new lines with BR tags:

如果是HTML,我会首先用BR标签替换新的行:

String str = str.Replace(Environment.NewLine, "<br/>");

either that, or instead of using a multiline textarea, use a JQuery or AJAX HTML Editor or something.

要么使用JQuery或AJAX HTML编辑器,要么使用多行文本区域。


Maybe you could swap in a place holder (like above, and then swap it out?)

也许你可以在一个占位符(如上面所示,然后把它交换出去?)

#1


3  

Outlook removes new lines unless the line ends with two spaces.

Outlook删除新的行,除非行以两个空格结束。

If you're testing the emails to an account that uses Outlook try adding two spaces before your new lines and see if that fixes it.

如果您正在测试一个使用Outlook的帐户的电子邮件,请尝试在新行之前添加两个空格,看看这是否能修复它。

#2


0  

If its for HTML I would first of all replace the new lines with BR tags:

如果是HTML,我会首先用BR标签替换新的行:

String str = str.Replace(Environment.NewLine, "<br/>");

either that, or instead of using a multiline textarea, use a JQuery or AJAX HTML Editor or something.

要么使用JQuery或AJAX HTML编辑器,要么使用多行文本区域。


Maybe you could swap in a place holder (like above, and then swap it out?)

也许你可以在一个占位符(如上面所示,然后把它交换出去?)