66

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?

6 Answers 6

124

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

2
  • Thanks Jon. "serving from Mono"? What's that?
    – jim
    Jul 27, 2009 at 9:11
  • 3
    As in, "Deploying a server using Mono on Linux, and asking that server to serve the data." Mono is a cross-platform implementation of .NET - see mono-project.com
    – Jon Skeet
    Jul 27, 2009 at 9:13
24

Use Environment.NewLine for line breaks.

14

Try this :

string myStr = ...
myStr = myStr.Replace("\n", Environment.NewLine)
4
  • 9
    if input string contains both \n and \r\n such operation will break it Jul 27, 2009 at 8:51
  • "I get one long text string with [] square looking symbols where the line breaks should be." Looks like it's not the case.
    – Guillaume
    Jul 27, 2009 at 9:03
  • 7
    @Guillaume: It's not the case this time, but there's no reason why a file can't contain two or more different kinds of line separator--I see it in web pages all the time. When you're normalizing newlines you should always match all three kinds: Regex.Replace(@"\n|\r\n?", "\r\n")
    – Alan Moore
    Jul 29, 2009 at 3:08
  • 1
    When you are noemalizing newlines from a non standard source ok. But when you KNOW you will ONLY have '\n' you don't need a regex...
    – Guillaume
    Jul 29, 2009 at 8:13
2

Also:

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

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);
}
1
  • I don't understand the connection from your strange task to the topic. Apr 14 at 8:40
0

As mentioned before it's better to use Environment.NewLine.
\n represents the newline character and is commonly used for line breaks in Unix-like systems (such as Linux and macOS) and in many programming languages.

Environment.NewLine is a platform-specific string that represents the newline sequence for the current operating system. It automatically adapts to the newline convention used on the platform where the code is running. On Windows, Environment.NewLine is equivalent to \r\n, while on Unix-like systems, it is equivalent to \n.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.