StringBuilder in c#
the following code snippet displays the fun with stringbuilder class of c#
using System;
using System.Windows.Forms;
using System.Text;
namespace StringBuilderStuff
{
class StringBuilderStuff
{
static void Main(string[] args)
{
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder("dsfghgh loves fffff!");
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];
objectArray[0] = "Mercedes Benz";
objectArray[1] = 129000.99;
sb1.AppendFormat(formstr, objectArray);
output = "sb1 = ";
output += sb1.ToString();
output += "\nsb2 = " + sb2.ToString() +
"\nLength = " + sb2.Length +
"\nCapacity = " + sb2.Capacity;
sb2.EnsureCapacity(75);
output += "\nNew capacity = " + sb2.Capacity;
output += "\nNew girlfriend = " +
sb2.Replace("loves Adelheide", "now loves Irmtraut");sb2.Length = 7;
output += "\nNew length = " + sb2.Length + "\nsb1 = ";
for (int i = 0; i < sb2.Length; i++)
output += sb2[i];
output += "\n\n";
sb3.Append("You owe me ");
sb3.Append("$");
sb3.Append(cost);
output += "sb3 = ";
output += sb3.ToString() + '\n';
sb4.Insert(0, cost);
sb4.Insert(0, "$");
sb4.Insert(0, "You owe me ");
output += "sb4 = ";
output += sb4.ToString();
output += "\nSorry!\n";
sb4.Remove(14, 3);
output += sb4.ToString();
MessageBox.Show(output, "StringBuilder stuff",
MessageBoxButtons.OK);
}
}
}