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

后端 未结 10 1498
无人及你
无人及你 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 03:45

    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.

提交回复
热议问题