C# 用换行符编码文本字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1187121/
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-06 10:20:08  来源:igfitidea点击:

C# Encoding a text string with line breaks

c#encodingresponse

提问by jim

in C# I have a string I am writing to the outputstream of the response. After I save this document and open it in Notepad++ or WordPad I get nicely formatted line breaks where they are intended, when I open this document with the regular old windows notepad, I get one long text string with [] square looking symbols where the line breaks should be.

在 C# 中,我有一个字符串要写入响应的输出流。在我保存这个文档并在 Notepad++ 或 WordPad 中打开它后,我在它们想要的地方得到了格式很好的换行符,当我用普通的旧 Windows 记事本打开这个文档时,我得到一个长文本字符串,其中有 [] 方形符号,行休息应该是。

Has anyone had any experience with this?

有没有人有这方面的经验?

Thanks

谢谢

Jim

吉姆

采纳答案by Jon Skeet

Yes - it means you're using \nas the line break instead of \r\n. Notepad only understands the latter.

是的 - 这意味着您使用的\n是换行符而不是\r\n. 记事本只能理解后者。

(Note that Environment.NewLinesuggested by others is fine if you want the platform default - but if you're serving from Mono and definitely want \r\n, you should specify it explicitly.)

(请注意,Environment.NewLine如果您想要平台默认值,其他人的建议是可以的 - 但如果您从 Mono 提供服务并且肯定想要\r\n,则应该明确指定它。)

回答by Guillaume

Try this :

尝试这个 :

string myStr = ...
myStr = myStr.Replace("\n", Environment.NewLine)

回答by Tadas ?ukys

Use Environment.NewLinefor line breaks.

使用Environment.NewLine换行。

回答by Pedro Miranda

Try \n\n, it will work! :)

尝试\n\n,它会起作用!:)

public async Task AjudaAsync(IDialogContext context, LuisResult result){
await context.PostAsync("How can I help you? \n\n 1.To Schedule \n\n 2.Consult");
context.Wait(MessageReceived);
}