65

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?

jim
  • 26,598
  • 13
  • 51
  • 66

6 Answers6

121

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks Jon. "serving from Mono"? What's that? – jim Jul 27 '09 at 09: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 http://mono-project.com – Jon Skeet Jul 27 '09 at 09:13
24

Use Environment.NewLine for line breaks.

Tadas Šukys
  • 4,140
  • 4
  • 27
  • 32
14

Try this :

string myStr = ...
myStr = myStr.Replace("\n", Environment.NewLine)
Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • 9
    if input string contains both \n and \r\n such operation will break it – abatishchev Jul 27 '09 at 08: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 '09 at 09: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 '09 at 03: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 '09 at 08:13
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);
}
2

Also:

string s = $"first line{Environment.NewLine}second line";
MachPlomo
  • 21
  • 2
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.

I.sh.
  • 252
  • 3
  • 13