7

Duplicate from : String output: format or concat in C#?

Especially in C# world using String.Format for everything is really common, normally as VB.NET developer unless I have to* I don't String.Format,

I prefer normal string concatenation, such as:

V1 = V2 & "test-x" & V3 & "-;"

to me it's better than this:

V1 = String.Format("{0} test-x {1} -;", V2, V3)

Am I missing something? Or is this just a personal preference?

Reasons to Use String.Format (From The Answers) (I'll try to keep this up to date)

  • Localization is so much easier if you use String Format
  • Obviously it's easier to change the format of input
  • It's more readable (however this is personal)
  • Better Performance

**Sometimes I need to change the style or replacing stuff dynamically then I use String.Format*

Community
  • 1
  • 1
dr. evil
  • 26,944
  • 33
  • 131
  • 201

8 Answers8

14

If you're ever going to localize your application (and it's often hard to rule that out at the start), then String.Format is to be much preferred, for two reasons:

  1. You have only one string literal to translate
  2. You can change the order of the values, which may make more sense in another language.
Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • that's pretty good reason to go with String.Format, thanks for pointing this out. – dr. evil Dec 08 '08 at 14:24
  • "You can change the order of the values" - How is that going to help you in another language? – Tomalak Dec 08 '08 at 14:25
  • First thing coming on my mind: Adresses, which are formatted in different ways all over the world (e.g. zip first, then city - or vice versa)... – Matthias Meid Dec 08 '08 at 14:28
  • 1
    "You can change the order of the values" > It's actually damn important because languages got different grammars where sometimes you have to reverse the sentence make sense in another language. One of the barriers I bumped into in an ASP application ages ago. – dr. evil Dec 08 '08 at 14:29
5

Everyone has posted about how readable string.format is (which I accept, and it has null ref and internationalisation benefits yes) but nobody has mentioned that it's considerably slower than simple string concatenation (small number of elements) or using StringBuilder (large number of concats).

If performance matters or you're doing a large number of operations (so performance soon will matter) then you should avoid format.

Edit: References as requested ;)

http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx

http://blog.briandicroce.com/2008/02/04/stringbuilder-vs-string-performance-in-net/

annakata
  • 74,572
  • 17
  • 113
  • 180
  • Feel free to reference http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx in your answer :) – Jon Skeet Dec 08 '08 at 14:25
  • Ha! That very article is what made me benchmark it myself a couple of months ago. Names are forgettable but content is remembered. – annakata Dec 08 '08 at 14:31
3

Nice article here by Karl Seguin: code better - use string.format explaining some of the benefit.

Galwegian
  • 41,475
  • 16
  • 112
  • 158
  • 1
    so basically it just looks better :) or readable which boils down to personal preference, isn't it ? – dr. evil Dec 08 '08 at 14:22
1

The first method is very hard to read and even more of a bore to type. As well, once you start doing a lot of those concatenations, there are performance considerations to think about.

BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
  • I thought they were exactly same, is there any real performance issues ? – dr. evil Dec 08 '08 at 14:18
  • Yep. Since strings are immutable in .NET, each concatenation creates a new string which must be collected by the GC. string.Format's approach is completely different (more StringBuilder-like). – TToni Dec 08 '08 at 14:24
  • good stuff, learned something new today. – dr. evil Dec 08 '08 at 14:25
  • 2
    @TToni: No, the compiler calls String.Concat with the appropriate strings. It's not like a + b + c + d involves 3 concatenation steps. – Jon Skeet Dec 08 '08 at 14:26
1

If you are actually formatting values ({0:d} etc.), String.Format is a lot better to read than string concatenation.

MZywitza
  • 3,121
  • 1
  • 17
  • 12
1

For me it depends on the contents. Concatenating strings does create additional string objects (because strings are immutable in .NET), though mainly it's a readbility issue.

Sometimes it gets complicated when you want to place newline characters in a string, in which case I tend to use something like:

StringBuilder.AppendLine(string.Format("Some text {0}.", "here"));
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
  • Bad example - use AppendFormat instead, then AppendLine with no args. – Jon Skeet Dec 08 '08 at 14:23
  • Y'know, you make a good point - I've never really been able to choose the "best" way to use StringBuilder.AppendLine/AppendFormat methods. I'd really like an AppendLineFormat method. :) Do you mean this: body.AppendFormat("Ths {0}", "sfdsf").AppendLine(); – Neil Barnwell Dec 08 '08 at 14:26
  • @Neil: Yup, although I'd usually make it a separate statement as well, unless I'm doing a *lot* of this. – Jon Skeet Dec 08 '08 at 14:38
0

Personally I find that String.Format is easier to read, the string is presented as one consecutive text. It depends though on how many parameters there are, if you need to hunt for the right parameter to understand it, then ...

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
0

I believe its more of a matter of preference. However once I started using it regularly most of my team followed suit. When we discussed it it was agreed that is just easier to understand and read.

Drak
  • 73
  • 1
  • 5