Click here to Skip to main content
Click here to Skip to main content

String.Format vs. String.Concat

, 4 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
You might think both do the same job, but does they?

Introduction

Both can be used for concatenating the strings, but the purpose of using both is totally different. As per the MSDN

  • String.Format: Replaces each format item in a specified string with the text equivalent of a corresponding object's value.
    • String.Concat: Concatenates one or more instances of String, or the String representations of the values of one or more instances of Object.

Background

By the way the best bet for doing the string operations like concatenation is by using the StringBuilder class of System.Text. Anyway that’s a different story for now.

I found many articles comparing the String.Concat and String.Format. Some says first one is better, but some thinks the later one is better. So, I decided to give them my touch. So, here is my findings.

I am reading many words from the file Seabiscuit.txt and later concatenating those words. I found that String.Concat is much faster than String.Format. If you implement the Parallel programming of .NET 4.0 then String.Cancat works much faster.


So why one should use the String.Format?

You can use the String.Format to format the values. You can find the interesting article here[^]

Using the Code

I have used a text file called Seabiscuit.txt and you can download it from here[^]. Yes the complete page text:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
namespace LearnConsole
{
  class Program
    {
	static void Main( string[] args )
	{
	StreamReader fs = new StreamReader( @"D:\Seabiscuit.txt" );
	try
	{
		string[] strSeaBiscuit = fs.ReadToEnd().ToString().Split(' ');
		string strCombined = string.Empty;
		DateTime dtStart = DateTime.Now;
		foreach ( string str in strSeaBiscuit )
		{
			strCombined = String.Concat( strCombined, str, " " );
		}
		Console.WriteLine( "String.Concat : " + -(dtStart.Subtract( DateTime.Now )		) );
		strCombined = string.Empty;
		dtStart = DateTime.Now;
		foreach ( string str in strSeaBiscuit )
		{
			strCombined = String.Format( "{0}{1} ", strCombined, str );
		}
		Console.WriteLine( "String.Format : " + -(dtStart.Subtract( DateTime.Now )) );
		strCombined = string.Empty;
		dtStart = DateTime.Now;
		System.Threading.Tasks.Parallel.ForEach( strSeaBiscuit, str =>
		{
			strCombined = String.Concat( strCombined, str, " " );
		}
		);
		Console.WriteLine( "Using Parallel String.Concat : " + -(dtStart.Subtract( DateTime.Now )) );
		strCombined = string.Empty;
		dtStart = DateTime.Now;
		System.Threading.Tasks.Parallel.ForEach( strSeaBiscuit, str =>
		{
			strCombined = String.Format( "{0}{1} ", strCombined, str );
		}
		);
		Console.WriteLine( "Using Parallel String.Format : " + -(dtStart.Subtract( DateTime.Now )) );
	}
	finally
	{
		if (fs != null)
		{
		fs.Close();
		}
	}
	Console.ReadKey();
	}
    }
}

Output

String.Concat : 00:00:00.0860086
String.Format : 00:00:00.1540154
Using Parallel String.Concat : 00:00:00.0840084
Using Parallel String.Format : 00:00:00.0850084

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sun1programmer
Team Leader IBM India Pvt. Ltd. Pune
India India
Work in Windows Forms, ASP.NET 2.0, 3.5, Portal Managements using DotNetNuke 4x., Rainbow, Windows Mobile Apllication. Having good knowledge of OOPs. Leading the team and using the cutting edge technologies for designing core components of application.
 
Having MCPD in Web Developement, MCITP Database Developer, MCTS in .NET 2.0: Web Application and SQL Server 2005.
 
Having good knowledge of Crystal Report 8.5 onwards.

Comments and Discussions

 
GeneralReason for my vote of 3 Check my comment with three correcti... Pinmemberhalex20057-Oct-10 9:35 
GeneralI think that time results have an errors of calculation. 1)... Pinmemberhalex20057-Oct-10 9:32 
GeneralReason for my vote of 5 Something many of us probably never ... PinmemberMike Barreyre7-Oct-10 4:31 
GeneralA very valuable study which I will remember. Much thanks! PinmemberMike Barreyre7-Oct-10 4:31 
Generalgood to know - thank you Pinmemberjohannesnestler5-Oct-10 6:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 4 Oct 2010
Article Copyright 2010 by sun1programmer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid