How to remove newLines at the beginning and at the end of a Ruby string

content01 picture content01 · Aug 23, 2011 · Viewed 17.9k times · Source

I need to remove newlines at the beginning and at the end of a string in ruby (some sort of trimming).

But JUST at the beginning and the end... The new lines located in the middle of the string must remain untouched.

Thank you!

Answer

Daniel O'Hara picture Daniel O'Hara · Aug 23, 2011

You can use String#strip method.

"\tgoodbye\r\n".strip   #=> "goodbye"