Environment NewLine

August 3, 2006

If you are one of those people that put “\r\n” in your strings to indicate a newline, raise your right hand and smack your head.

Why?

1) You are assuming your code will always run on Windows. Unix uses \n alone, Apple (until OS 9) used \r alone. Your code might just end up being compiled under Mono and then what?
2) It looks plain ugly
3) You have to escape the characters again (at least in C# if you’re not using the verbatim operator @). This quickly becomes a pain in the — er –becomes cumbersome when dealing with a lot of text

What should you do?

Simple

string line = string.Format(“And this is the end of the line{0}”, Environment.NewLine);

kick it on DotNetKicks.com

Advertisements

8 Responses to “Environment NewLine”


  1. Good point. I also find it plain ugly. You can achieve the same in a StringBuilder:

    StringBuilder sb = new StringBuilder();
    sb.AppendLine(“And this is the end of the line”)

    Or the not-so-pretty version:

    string line = “And this is the end of the line” + Environment.NewLine


  2. […] I just found a site (via dotnetkicks.com) that is advocating the use of Environment.NewLine. […]


  3. I started using the String.Format(); method about a month ago (prior to that, I used the + for concatenation). Using Environment.Newline is much nicer.

    Need 2 linefeeds? sR = String.Format(“Hello World!{0}{0}”, Environment.Newline);

  4. Alfred H. Says:

    Amazing to be visiting your web again, it has been many weeks for me. Fine this article that i am waited for too long. I would like this content to achieve my assignment in the college, it contains similar topic with your post. Thanks, great discuss this.


  5. color in fashion description http://clothingtrends.eu/red-color12.html fashion illustration sketch 2600706


  6. I’m not sure exactly why but this site is loading very slow for me. Is anyone else having this problem or is it a issue on my end? I’ll check back later on and see if the problem still
    exists.

  7. Kathi Says:

    No matter if some one searches for his required thing, thus he/she needs to be
    available that in detail, therefore that thing is maintained over here.


  8. Hoverboard was inspired by other Flux implementations, like Redux , Alt and Reflux Those versions are quite lightweight, but
    Hoverboard is virtually weightless.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: