.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

performance of a concatenate string.

Posted By:Syed Shakeer Hussain       Posted Date: September 03, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  
 

From this Article you can know which is the best way in performance to concatenate a string.
we can concatenate a string in two ways.
1)by using String
2)by using StringBuilder class.


1)String Explanation
:Normally  two strings can concatenate as below

String str1="A";
String str2="B";
String str3=str1+str2
;

it is a simple way if there is less concatenation.if you want to concatenate string n number of times by using forloop it takes time to complete entire loop and result for output.

2)StringBuilder is the best way to concatenate string.it doesnt take any time to concatenate string compare to normal string concatenate.you can concanate a string by using Append()  as follows
example

StringBuilder sb1 = new StringBuilder("Shakeer");
           sb1.Append("Hussain");
           Console.WriteLine(sb1.ToString());


output:ShakeerHussain

To see which is the best way to use String and StringBuilder see Below coding and OutPut:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace diffString
{
    class Program
    {
        static void Main(string[] args)
        {
          
            string str = "";
            Console.WriteLine("Starting time for concatenate string  =" + System.DateTime.Now.ToLongTimeString());
           for (int i = 0; i < 20000; i++)
           {
                str +=i.ToString (); 
           }
           Console.WriteLine("Ending  time for concatenate string  =" + System.DateTime.Now.ToLongTimeString());
           Console.WriteLine("");
           Console.WriteLine("Starting time for concatenate string with stringbuilder =" + System.DateTime.Now.ToLongTimeString());
             for (int i = 0; i < 20000; i++)
           {
           
               StringBuilder sb = new StringBuilder("shakeer");
               sb.Append(i.ToString ());
              
           }

           Console.WriteLine("Ending time for concatenate string with stringbuilder =" + System.DateTime.Now.ToLongTimeString());

            Console.Read();
        }
       
    }
}

OutPut

 

In the above output observe that it takes 4 seconds to concatenate norma String.By using StringBuilder Class it does not take any time concatenate string.
So use StringBuilder is the better way to concatenate String.

Note:if you are using StringBuilder Class you have to add a NameSpace Using System.Io


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend