We help IT Professionals succeed at work.

Trimming \r\n from end of string

What's the C# syntax for this?
Comment
Watch Question

if(yourString.EndsWith("\r\n") == true)//then trim off the last 2 characters.
{
    yourString = yourString.Substring(0, yourString.Length - 2);
}
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Just use Trim()...

    str = str.Trim();
anarki_jimbelSenior Developer
Commented:
With the first approach: what will you do if you have ten "new lines"? Or hundred?
Like: s = "abc\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"

Trim removes any "white spaces": space, tab, new line.
You can use trim method with parameters to trim any other characters.
curiouswebsterSoftware Engineer

Author

Commented:
What params do I use for trim, in this case?
anarki_jimbelSenior Developer

Commented:
Can you please check my solution for other thread? It shows an example:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27460327.html
curiouswebsterSoftware Engineer

Author

Commented:
Thanks. But I know how to use Trim().

I am having a hard time trimming the \r\n char.  Or is it two characters.

Please give an example of trimming these.

Thanks.
anarki_jimbelSenior Developer

Commented:
But Idle_Mind gave the solution!

Just use Trim()...

    str = str.Trim();

This should remove \r\n,  I'm 100% confident. If it does not happen in your case probably you have some other character at the end of a string, like dot "." or something else.

And yes, \r\n - these are TWO characters, the first is "cartridge return", the other - "line feed".
But both are "white spaces" and Trim would remove them .
curiouswebsterSoftware Engineer

Author

Commented:
Thanks. Sorry for the misunderstanding.
anarki_jimbelSenior Developer

Commented:
We are here to help :)