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

后端 未结 10 1495
无人及你
无人及你 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

    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();
    

提交回复
热议问题