Search code examples
c#stringbuilder

Trim whitespace from the end of a StringBuilder without calling ToString().Trim() and back to a new SB


What is an efficient way to trim whitespace from the end of a StringBuilder without calling ToString().Trim() and back to a new SB new StringBuilder(sb.ToString().Trim()).


Solution

  • The following is an extension method, so you can call it like this:

    sb.TrimEnd();
    

    Also, it returns the SB instance, allowing you to chain other calls (sb.TrimEnd().AppendLine()).

    public static StringBuilder TrimEnd(this StringBuilder sb)
    {
        if (sb == null || sb.Length == 0) return sb;
    
        int i = sb.Length - 1;
    
        for (; i >= 0; i--)
            if (!char.IsWhiteSpace(sb[i]))
                break;
    
        if (i < sb.Length - 1)
            sb.Length = i + 1;
    
        return sb;
    }
    

    Notes:

    1. If Null or Empty, returns.

    2. If no Trim is actually needed, we're talking a very quick return time, with probably the most expensive call being the single call to char.IsWhiteSpace. So practically zero expense to call TrimEnd when not needed, as opposed to these ToString().Trim() back to SB routes.

    3. Else, the most expensive thing, if trim is needed, is the multiple calls to char.IsWhiteSpace (breaks on first non-whitespace char). Of course, the loop iterates backwards; if all are whitespace you'll end up with a SB.Length of 0.

    4. If whitespaces were encountered, the i index is kept outside the loop which allows us to cut the Length appropriately with it. In StringBuilder, this is incredibly performant, it simply sets an internal length integer (the internal char[] is kept the same internal length).

    Update: See excellent notes by Ryan Emerle as follows, which correct some of my misunderstandings (the internal workings of SB are a little more complicated than I made it out to be):

    The StringBuilder is technically a linked list of blocks of char[] so we don't end up in the LOH. Adjusting the length isn't technically as simple as changing the end index because if you move into a different chunk the Capacity must be maintained, so a new chunk may need to be allocated. Nevertheless, you only set the Length property at the end, so this seems like a great solution. Relevant details from Eric Lippert: https://stackoverflow.com/a/6524401/62195

    Also, see this nice article discussing the .NET 4.0 new StringBuilder implementation: http://1024strongoxen.blogspot.com/2010/02/net-40-stringbuilder-implementation.html

    Update: Following illustrates what happens when a StringBuilder Length is altered (the only real operation done to the SB here, and that only when needed):

    StringBuilder sb = new StringBuilder("cool  \t \r\n ");
    
    sb.Capacity.Print(); // 16
    sb.Length.Print();  // 11
            
    sb.TrimEnd();
    
    sb.Capacity.Print(); // 16
    sb.Length.Print();  // 4 
    

    You can see the internal array (m_ChunkChars) stays the same size after changing the Length, and in fact, you can see in the debugger it doesn't even overwrite the (in this case whitespace) characters. They are orphaned is all.