Which is the Better way to Concatenate Strings?

Syedshakeer
Posted by in C# category on for Intermediate level | Views : 6399 red flag

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

Page copy protected against web site content infringement by Copyscape

About the Author

Syedshakeer
Full Name: Syed Shakeer Hussiain P
Member Level:
Member Status: Member
Member Since: 2/5/2009 3:12:18 AM
Country: India
Syed Shakeer Hussain
http://www.dotnetfunda.com
Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)