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)
**Sometimes I need to change the style or replacing stuff dynamically then I use String.Format*
String formatting uses a process of string interpolation (variable substitution) to evaluate a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
Put another way, using StringBuilder here is over 40 TIMES faster than String. format() . This is probably quite a bit bigger difference than most Java developers would have guessed.
Avoid using String. format() when possible. It is slow and difficult to read when you have more than two variables.
String. format("%4.3f" , x) ; It means that we need total 4 digits in ans , of which 3 should be after decimal . And f is the format specifier of double .
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:
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With