Search code examples
c#stringspecial-characters

Stop C# string.Trim() removing FF character?


Is there any way of stopping string.Trim() from removing an FF character?

I am parsing a file wanrt to be able to tell the difference between an empty line with just an FF char, a line of spaces with an FF char, and a line of "data" with an FF char.

However, if I test string.Trim() == string.Empty then it returns true for all of:

"\f"
"                               "
"                             \f"

e.g. It is also trimming the \f out of the string?

Note: I can test for the \f using Regex, but interested in why the trim removes the \f as well?


Solution

  • From the MSDN:

    string.trim

    White-space characters are defined by the Unicode standard. The Trim() method removes any leading and trailing characters that produce a return value of true when they are passed to the Char.IsWhiteSpace method.

    http://msdn.microsoft.com/en-us/library/t97s7bs3.aspx

    From Char.IsWhiteSpace

    White space characters are the following Unicode characters: The characters CHARACTER TABULATION (U+0009), LINE FEED (U+000A), LINE TABULATION (U+000B), FORM FEED (U+000C), CARRIAGE RETURN (U+000D), NEXT LINE (U+0085), and NO-BREAK SPACE (U+00A0).

    http://msdn.microsoft.com/en-us/library/t809ektx.aspx

    That's why it's getting stripped out.

    Hope this helps