What is the easiest way in C# to trim a newline off of a string?

后端 未结 10 1510
无人及你
无人及你 2020-12-13 03:11

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

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

10条回答
  •  爱一瞬间的悲伤
    2020-12-13 04:00

    _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.

提交回复
热议问题