mo.notono.us

Tuesday, June 24, 2008

.NET: What exactly does Trim() trim?

I found myself using the character specific String.Trim(params char[] trimChars) overload to remove some carriage returns and tabs when I realized I was reinventing the wheel again.  Not content with the basic Trim()'s intellisense ("Removes all occurrences of white space characters from the beginning and end of this instance") I dove into Reflector:

public string Trim()
{
    return this.TrimHelper(WhitespaceChars, 2);
}

TrimHelper is fairly obvious - it does the actual trimming of the characters.

The WhitespaceChars array (which is also used by TryParse() and some of the other TrimXxx() methods) is set in the String static constructor:

static String()
{
    Empty = "";
    WhitespaceChars = new char[] { 
        '\t', '\n', '\v', '\f', '\r', ' ', '\x0085', '\x00a0', ' ', 
        ' ', ' ', ' ', ' ', ' ', ' ', ' ', 
        ' ', ' ', ' ', ' ', '​', '\u2028', '\u2029', ' ', ''
     };
}

Not sure what some of those non-printable characters are, but I'm satisfied...

Labels: , , , ,

0 Comments:

Post a Comment

<< Home