Experiments with StringBuilder (C#)

vegaseat 0 Tallied Votes 181 Views Share

C# has two types of strings, one is immutable and the other, the class StringBuilder, is mutable (dynamic). I just wanted to hang a short code snippet out there to show you some of the typical operations.

// experimenting with class StringBuilder (creates dynamic/mutable strings)
// used SnippetCompiler.exe (which in turn uses csc.exe) to compile and run
// download free from: http://www.sliver.com/dotnet/SnippetCompiler/
// C# and .NET Framework V2.0     vegaseat    04apr2007 

using System;
using System.Windows.Forms;  // MessageBox
using System.Text;           // StringBuilder

namespace StringBuilderStuff
{
  class StringBuilderStuff
  {
    static void Main(string[] args)
    {
      // StringBuilder constructors ...
      StringBuilder sb1 = new StringBuilder();
      StringBuilder sb2 = new StringBuilder("Ludwig loves Adelheide!");
      StringBuilder sb3 = new StringBuilder();
      StringBuilder sb4 = new StringBuilder();
      double cost = 18.95;
      string output;
      string formstr = "This {0} costs: {1:C}\n";
      object[] objectArray = new object[2];       // for mixed types

      objectArray[0] = "Mercedes Benz";
      objectArray[1] = 129000.99;
      sb1.AppendFormat(formstr, objectArray);
      output = "sb1 = ";
      output += sb1.ToString();

      // use Length and Capacity properties
      output += "\nsb2 = " + sb2.ToString() +
        "\nLength = " + sb2.Length +
        "\nCapacity = " + sb2.Capacity;

      // use EnsureCapacity method
      sb2.EnsureCapacity(75);

      output += "\nNew capacity = " + sb2.Capacity;
      
      // use Replace method
      output += "\nNew girlfriend = " +
        sb2.Replace("loves Adelheide", "now loves Irmtraut");

      // truncate StringBuilder by setting Length property
      sb2.Length = 7;

      output += "\nNew length = " + sb2.Length + "\nsb1 = ";

      // use StringBuilder indexing
      for (int i = 0; i < sb2.Length; i++)
        output += sb2[i];
        
      output += "\n\n";
 
      // build string in normal order
      sb3.Append("You owe me ");
      sb3.Append("$");
      sb3.Append(cost);
      output += "sb3 = ";
      output += sb3.ToString() + '\n';     
      
      // build string in reverse order
      sb4.Insert(0, cost);
      sb4.Insert(0, "$");
      sb4.Insert(0, "You owe me ");
      output += "sb4 = ";
      output += sb4.ToString();
      
      output += "\nSorry!\n";
      // removes '.95' from cost
      sb4.Remove(14, 3);
      output += sb4.ToString();      

      MessageBox.Show(output, "StringBuilder stuff",
        MessageBoxButtons.OK);
    }
  }
}
Lardmeister 461 Posting Virtuoso

Thanks vegaseat, looks like it may be possible to teach C# after all. I am thinking of a hands-on course, where I can hand out assignments on various subjects, and expect single code file returns. This way I can best evaluate the student's performance and assist in the learning experience.

varagpraveen 0 Newbie Poster

Is It recommended to use StringBuilder.Append("STR1"+"STR2");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.