C# Encoding a text string with line breaks

C#EncodingResponse

C# Problem Overview


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, but when I open this document with the regular old Windows Notepad, I get one long text string with □ (square like symbols) where the line breaks should be.

Has anyone had any experience with this?

C# Solutions


Solution 1 - C#

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

(Note that Environment.NewLine suggested 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.)

Solution 2 - C#

Use Environment.NewLine for line breaks.

Solution 3 - C#

Try this :

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

Solution 4 - C#

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

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);
}

Solution 5 - C#

Also:

string s = $"first line{Environment.NewLine}second line";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjimView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Tadas ŠukysView Answer on Stackoverflow
Solution 3 - C#GuillaumeView Answer on Stackoverflow
Solution 4 - C#Pedro Henrique ArthurView Answer on Stackoverflow
Solution 5 - C#MachPlomoView Answer on Stackoverflow