Wednesday 11 April 2012

StringBuilder in C#


//String classs and StringBuilder Class Comparision
using System;
using System.Text;
class Program
{
static void Main()
{
StringBuilder sb=new StringBuilder();

Console.WriteLine("\n......................String Builder Example......................\n");

sb.Append("This is a example of string");

Console.WriteLine("\nActual String is: '{0}'",sb.ToString());

sb.Replace("string","StringBuilder");
Console.WriteLine("\nAfter Replace String is: '{0}'",sb.ToString());

sb.Insert(0,"Extra ");
Console.WriteLine("\nAfter Insert String is: '{0}'",sb.ToString());

sb.Remove(0,5);
Console.WriteLine("\nAfter Remove String is: '{0}'",sb.ToString());

Console.WriteLine("\nStart Time: {0}",DateTime.Now.ToString("T"));
for(int i=0;i<100000;i++)
sb.Append(i);
Console.WriteLine("\nEnd Time: {0}",DateTime.Now.ToString("T"));

Console.WriteLine("\n......................String Example......................\n");
string s1="";

Console.WriteLine("\nStart Time: {0}",DateTime.Now.ToString("T"));
for(int i=0;i<100000;i++)
s1=s1+i;
Console.WriteLine("\nEnd Time: {0}",DateTime.Now.ToString("T"));
}
}

No comments:

Post a Comment