C# 为什么 StringBuilder.AppendLine 不添加带有某些字符串的新行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11155801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:46:22  来源:igfitidea点击:

Why does StringBuilder.AppendLine not add a new line with some strings?

c#.netnewlinestringbuilder

提问by kolin

I'm trying to use stringbuilder to create a body of string to be used in a text (not HTML) email. However some lines (where i include dynamic data, a new line is not added, but in some the newline works as intended.

我正在尝试使用 stringbuilder 创建要在文本(不是 HTML)电子邮件中使用的字符串主体。然而,有些行(我包含动态数据,没有添加新行,但在某些行中,换行符按预期工作。

Is there something basic i'm missing when using the stringbuilder class or is there some more fundamental process that should be happening?

使用 stringbuilder 类时是否缺少一些基本的东西,或者是否应该发生一些更基本的过程?

in the below code:

在下面的代码中:

sbUser.AppendLine("Please find below confirmation of your registration details. If any of these details are incorrect, please email [email protected]");
sbUser.AppendLine();
sbUser.AppendLine("Selected event : " + ContentPage.FetchByID(int.Parse(ddlEvent.SelectedValue)).PageTitle); 
sbUser.AppendLine("Date of event : " + thisEvent.EventStartDate.ToString("dd MMM yyyy"));
sbUser.AppendLine("==============================================================");
sbUser.AppendLine();

(ContentPage and thisEvent are custom classes built using Subsonic(v2). PageTitle is an output type of string)

(ContentPage 和 thisEvent 是使用 Subsonic(v2) 构建的自定义类。PageTitle 是字符串的输出类型)

is get this as an output:

将其作为输出:

    Please find below confirmation of your registration details. If any of these details are incorrect, please email [email protected]

Selected event : My Event Date of event : 16 Sept 2012 ==============================================================

as you can see, everything after the 3rd line in the code makes everything go on to one line.

如您所见,代码中第 3 行之后的所有内容都使所有内容都转到一行。

however, further down the code i use:

但是,进一步向下我使用的代码:

sbRR.AppendLine("First name : " + txtFirstname.Text.Trim());
sbRR.AppendLine("Surname : " + txtSurname.Text.Trim());
etc,

and all these appear on seperate lines correctly. I can't see why this is happening.

所有这些都正确地出现在单独的行上。我不明白为什么会这样。

the email is composed as such

电子邮件是这样组成的

mailMessage.Body = sbUser.ToString() + sbRR.ToString();

mailMessage.Body = sbUser.ToString() + sbRR.ToString();



adding the following code:

添加以下代码:

sbUser.AppendLine("Selected event : " + ContentPage.FetchByID(int.Parse(ddlEvent.SelectedValue)).PageTitle + Environment.NewLine); 
sbUser.AppendLine("Date of event : " + thisEvent.EventStartDate.ToString("dd MMM yyyy") + Environment.NewLine);

produces the following output:

产生以下输出:

Selected event : My Event

Date of event : 16 Sept 2012

==============================================================

which works i suppose, except it's added 2 newlines (the AppendLine and the Environment.NewLine). it seems that pulling the data directly straight from the database into a stringbuilder seems to be messing with the line ending. Even if I add text after the database pull, it still stays on one line.

我想这是可行的,除了它添加了 2 个换行符(AppendLine 和 Environment.NewLine)。似乎将数据直接从数据库中直接拉入 stringbuilder 似乎与行尾混淆。即使我在数据库拉取后添加文本,它仍然停留在一行上。

UPDATE

更新

doing StringBuilder.Append("blah"+Environment.NewLine)produces the correct result, however i'm still not understanding why that works and .AppendLine("blah"+<database content>)doesn't work.

这样做 StringBuilder.Append("blah"+Environment.NewLine)会产生正确的结果,但是我仍然不明白为什么它有效而.AppendLine("blah"+<database content>)无效。

采纳答案by oleksii

Instead of

代替

sbUser.AppendLine();

Try using

尝试使用

sbUser.Append(Environment.NewLine);

No idea why this works...

不知道为什么这有效...

回答by Liam

use Environment.NewLine

Environment.NewLine

sbUser.AppendLine("Please find below confirmation of your registration details. If any of these details are incorrect, please email [email protected]");
sbUser.AppendLine(Environment.NewLine);
sbUser.AppendLine("Selected event : " + ContentPage.FetchByID(int.Parse(ddlEvent.SelectedValue)).PageTitle); 
sbUser.AppendLine("Date of event : " + thisEvent.EventStartDate.ToString("dd MMM yyyy"));
sbUser.AppendLine("==============================================================");
sbUser.AppendLine(Environment.NewLine);

回答by Nighil

use Environment.NewLineafter each line or where you want new line

使用Environment.NewLine每行后或您希望新行

eg:-

例如:-

  sbUser.AppendLine("Please find below confirmation of your registration details. If any of these details are incorrect, please email [email protected]" + Environment.NewLine);
  sbUser.AppendLine("Selected event : " + ContentPage.FetchByID(int.Parse(ddlEvent.SelectedValue)).PageTitle); 

回答by cjmurph

I know the question is old and has been marked as answered, but I thought I'd add this here in case anyone else comes across this as it's the first hit on Google for StringBuilder.AppendLine()not working.

我知道这个问题很旧并且已被标记为已回答,但我想我会在此处添加此内容,以防其他人遇到此问题,因为这是 Google 上第一次因StringBuilder.AppendLine()无法正常工作而受到影响。

I had the same problem and it turned out to be an Outlook issue. Outlook re-formats text based emails by removing extra line breaks. You can click "We removed extra line breaks in this message -> Restore line breaks" in the header of the individual email, or change the setting that does this nasty little trick "Options->Mail->Message Format->Remove extra line breaks in plain text messages"

我遇到了同样的问题,结果是 Outlook 问题。Outlook 通过删除额外的换行符来重新格式化基于文本的电子邮件。您可以单击单个电子邮件标题中的“我们删除了此消息中的额外换行符 -> 恢复换行符”,或更改执行此讨厌小技巧的设置"Options->Mail->Message Format->Remove extra line breaks in plain text messages"

The workaround (since you can't control the settings on every potential email target) I found here Newsletter Formatting And The Remove Extra Line Breaks Issue. Basically, if you add two white space characters to the beginning of each line, Outlook won't reformat the email.

解决方法(因为您无法控制每个潜在电子邮件目标的设置)我在这里找到了时事通讯格式和删除额外换行符问题。基本上,如果您在每行的开头添加两个空格字符,Outlook 将不会重新格式化电子邮件。

Here's an extension method to help (method name is a bit verbose so change to your liking :))

这是一个可以提供帮助的扩展方法(方法名称有点冗长,因此可以根据自己的喜好进行更改:))

namespace System.Text
{
    public static class StringBuilderExtensions
    {
        public static void AppendLineWithTwoWhiteSpacePrefix(this StringBuilder sb, string value)
        {
            sb.AppendFormat("{0}{1}{2}", "  ", value, Environment.NewLine);
        }

        public static void AppendLineWithTwoWhiteSpacePrefix(this StringBuilder sb)
        {
            sb.AppendFormat("{0}{1}", "  ", Environment.NewLine);
        }
    }
}

回答by Rolo

First sbUser.Appendline();

第一的 sbUser.Appendline();

Second sbUser.Append("texto loco ");

第二 sbUser.Append("texto loco ");

Voila!

瞧!

=)

=)

回答by ColinPBriggs

Windows 10 Insider preview Build 15007. The Default Line Terminator and the Environment.NewLineare both "\n". To?use "\r\n"I had to create a string constant and use it instead.

视窗10内幕预览构建15007.默认线路终端和Environment.NewLine"\n"。To?use"\r\n"我必须创建一个字符串常量并使用它。