How to insert newline in string literal?

前端 未结 12 786
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 09:07

In .NET I can provide both \\r or \\n string literals, but there is a way to insert something like \"new line\" special character like Enviro

12条回答
  •  鱼传尺愫
    2020-12-07 09:53

    If you want a const string that contains Environment.NewLine in it you can do something like this:

    const string stringWithNewLine =
    @"first line
    second line
    third line";
    

    EDIT

    Since this is in a const string it is done in compile time therefore it is the compiler's interpretation of a newline. I can't seem to find a reference explaining this behavior but, I can prove it works as intended. I compiled this code on both Windows and Ubuntu (with Mono) then disassembled and these are the results:

    Disassemble on Windows Disassemble on Ubuntu

    As you can see, in Windows newlines are interpreted as \r\n and on Ubuntu as \n

提交回复
热议问题