开发者

Concise way of outputting newlines in C#

In C++, I can do this:

cout << "Line 1\nLine 2\n";

In Java, I can do this:

Syste开发者_运维技巧m.out.printf("Line 1%nLine 2%n");

In C#, do I really need to do one of these cumbersome things:

Console.WriteLine("Line 1");
Console.WriteLine("Line 2");

or

Console.Write("Line 1{0}Line 2{0}", Environment.NewLine);

or is there a more concise way, which is not platform-specific?


No, there is no concise, platform-agnostic, built-in newline placeholder in C#.

As a workaround, you could create an extension method for Environment.NewLine

public static class StringExtensions()
{
    public static string NL(this string item)
    {
        return item += Environment.NewLine;
    }
}

Now you can use NL (picked because of brevity)

Console.Write("Hello".NL());
Console.Write("World".NL());

writes out:

Hello
World

You could also make an extension method that simply writes out something to the console.

public static void cout(this string item)
{
    Console.WriteLine(item);
    //Or Console.Write(item + Environment.NewLine);

}

And then:

"Hello".cout();


This should work:

Console.Write("Line 1\r\nLine 2");


On Windows, Environment.NewLine will just return "\r\n". So in your code you could do

Console.Write("Line 1\r\nLine 2\r\n");

Or simply

Console.Write("Line 1\nLine 2\n");

still works on most platforms. But otherwise you'll have to use the Environment.NewLine, or another similarly implemented and shorter named method, to return the correct string.


Normally I would recommend creating an extension method on the Console class, but that's not possible since Console is static. Instead, you could create a ConsoleHelper:

public static class ConsoleHelper
{
    public static void EnvironmentSafeWrite(string s)
    {
        s = Environment.NewLine == "\n" ? s : s.Replace("\n", Environment.NewLine);
        Console.Write(s);
    }
}

And use it like this:

ConsoleHelper.EnvironmentSafeWrite("Line 1\nLine 2\n");


Have you tried the following?

Console.Write("Line 1\nLine2");

Depending on the environment you may need to use \r\n.


Console.WriteLine() will simply output a newline without requiring other text.

You can change the defined string for NewLine by setting Console.Out.NewLine = "your string here";


\r\n Can be used on Windows platforms. However you question doesn't state which platform you are targeting. If you want your code to be multi-platform and future proof is probably safer to use Environment.NewLine


Just to offer yet another workaround:

With string interpolation (C# 6.0 and above), you can use

Console.Write($"Line 1{Environment.NewLine}Line 2{Environment.NewLine}");

Unfortunately, you can't define a constant for Environment.NewLine with a short name, since Environment.NewLine is not constant, but you can use a static readonly property as the next best thing:

private static readonly string NL = Environment.NewLine;

...

void someMethod()
{
    ...
    Console.Write($"Line 1{NL}Line 2{NL}");
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜