Example

using System;
using System.Text;

namespace Samples
{
  public class StringBuilderSample
  {
    public static void Main()
    {
      StringBuilder sb = new StringBuilder("Hello");
      Console.WriteLine("Capacity: {0}",
                        sb.Capacity);
      Console.WriteLine("MaxCapacity: {0}",
                        sb.MaxCapacity);
      Console.WriteLine("Length: {0}",
                        sb.Length);
      sb.Append(" world! ");
      sb.Insert(11, " from DAMIEN");
      sb.Replace("DAMIEN", "damien");
      sb.AppendFormat("appended at {0:dd/MM/yy}",
                      DateTime.Now);
      string s = sb.ToString();
      sb.Remove(11, 13);
      Console.WriteLine(s);
      Console.WriteLine(sb);
    }
  }
}
The output is
Capacity: 16
MaxCapacity: 2147483647
Length: 5
Hello world from damien! appended at 23/06/03
Hello world appended at 23/06/03

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.