.NET "default line terminator"?
Asked Answered
P

3

28

Is there any way to figure out what .NET is using as its "default line terminator"? For example, documentation for StringBuilder.AppendLine(String) says it "Appends a copy of the specified string followed by the default line terminator...". Several text-related classes in .NET refer to the same concept.

Is there any way to programmatically figure out what is being used as the line terminator (at runtime)? Or is it safe to assume that it will always be "\r\n" for a Windows machine? I'd rather not hard-code that value into my code if I can avoid it.

Pecten answered 8/10, 2009 at 16:3 Comment(0)
S
49

StringBuilder.AppendLine will use Environment.NewLine, which is "\r\n" for non-Unix platforms and "\n" for Unix platforms.

It will always be "\r\n" for a Windows machine, but you can use Environment.NewLine in lieu of a hard-coded value.

thanks to @Guffa for verifying this

Spadix answered 8/10, 2009 at 16:4 Comment(2)
Ah perfect, I knew there must be something like that. Thanks!Pecten
wish there was a way to change the default line terminator for a particular StringBuilder objectDamnedest
D
21

This is the code from the StringBuilder.AppendLine(string) method (using .NET Reflector):

[ComVisible(false)]
public StringBuilder AppendLine(string value)
{
   this.Append(value);
   return this.Append(Environment.NewLine);
}

As you see, it's indeed using the Environment.NewLine property.

Dinorahdinosaur answered 8/10, 2009 at 16:17 Comment(0)
C
3

On Mac, the newline was '\r' that is until they based their OS on linux. Now its '\n'.

Cai answered 9/11, 2009 at 6:38 Comment(1)
Just a minor nit-pick: today's Mac OS is actually based on UNIX, not linux. And the first UNIX-based version came our in early 2001, about a year before .NET was released. So the '\r' notation has not been relevant for some time.Pecten

© 2022 - 2024 — McMap. All rights reserved.