tags:

views:

1361

answers:

8

I want to make sure that _content does not end with a NewLine character:

_content = sb.ToString().Trim(new char[] { Environment.NewLine });

but the above code doesn't work since Trim seems to not have an overloaded parameter for a collection of strings, only characters.

What is the simplest one-liner to remove an Enivronment.Newline from the end of a string?

+2  A: 

What about

_content = sb.ToString().Trim(Environment.NewLine.ToCharArray());
heavyd
That doesn't do *quite* the same thing - it'll trim "abc\n\n\n\n\n" for example, even if Environment.NewLine is crlf
Marc Gravell
Declare it as a feature :-) I like the solution.
Stefan Steinegger
A: 

Why not use regex?

StuperUser
I don't belive RegEx is the optimal way in this "simple" string manipulation case.
bang
Understandably, and Edward is asking for a native C# solution. I should've been more specific; "Why in this case, is a native C# solution more desirable than using regex? Are there other cases where native text manipulation is more desirable than regex."I'll have a look for a question like that now. :)
StuperUser
For simple problems, native text manipulation is easier to read than regex, as well as more efficient. When it stops being easier to read (e.g. in the case of more complex expressions), regex may be more appropriate.
Brian
Question for Regex vs Native string manipulation: http://stackoverflow.com/questions/1038186/are-there-particular-cases-where-native-text-manipulation-is-more-desirable-than
StuperUser
+4  A: 

How about:

public static string TrimNewLines(string text)
{
    while (text.EndsWith(Environment.NewLine))
    {
        text = text.Substring(0, text.Length - Environment.NewLine.Length);
    }
    return text;
}

It's somewhat inefficient if there are multiple newlines, but it'll work.

Alternatively, if you don't mind it trimming (say) "\r\r\r\r" or "\n\n\n\n" rather than just "\r\n\r\n\r\n":

// No need to create a new array each time
private static readonly char[] NewLineChars = Environment.NewLine.ToCharArray();

public static string TrimNewLines(string text)
{
    return text.TrimEnd(NewLineChars);
}
Jon Skeet
Probably the most accurate solution, but not a one-liner :-)
bang
the top one works, needed to change "original" to "text" though
Edward Tanguay
Whoops - changed code half way through writing it. Am on phone now - could someone else fix please?
Jon Skeet
+1  A: 
_content = sb.TrimEnd(Environment.NewLine.ToCharArray());

This will of course remove "\r\r\r\r" as well as "\n\n\n\n" and other combinations. And in "enviroments" where NewLine is other than "\n\r" you might get some strange behaviors :-)

But if you can live with this then I belive this is the most effectiv way to remove new line characters at the end of a string.

bang
+12  A: 

The following works for me.

sb.ToString().TrimEnd( '\r', '\n' );

or

sb.ToString().TrimEnd( Environment.NewLine.ToCharArray());
Simon Wilson
very concise and deals with the \r\n issue, nice
Edward Tanguay
A: 

Use the Framework. The ReadLine() method has the following to say:

A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r") or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed.

So the following will do the trick

_content = new StringReader(sb.ToString()).ReadLine();
Scott Weinstein
A: 

How about just:

string text = sb.ToString().TrimEnd(null)

That will pull all whitespace characters from the end of the string -- only a problem if you wanted to preserve non-newline whitespace.

Richard Dunlap
A: 

Somewhat of a non-answer, but the easiest way to trim a newline off of a string is to not have the newline on the string in the first place, by making sure it is is never seen by your own code. That is, by using native functions which remove the newline. Many stream and file/io methods will not include the newline if you ask for output line by line, though it may be necessary to wrap something in a System.IO.BufferedStream.

Things like System.IO.File.ReadAllLines can be used in place of System.IO.File.ReadAllText most of the time, and ReadLine can be used instead of Read once you are working with the right type of stream (e.g. BufferedStream).

Brian