Simple trick to Trim the last character off of a StringBuilder

Posted by Blake on 4/30/2013
)

This isn’t a full blown Trim/TrimLast/TrimEnd that will Trim a character of given set of characters, but it will Trim off the last character and is very handy (since a lot of times, we just want to remove the last character regardless of what it is).

VB.Net

    Dim sb As New StringBuilder
    sb.Append("This is a test!")
    sb.Length = sb.Length - 1

C#

    StringBuilder sb = new StringBuilder();
    sb.Append("This is a test!");
    sb.Length = sb.Length - 1;