Why use String.Format? [duplicate]
Why would anyone use String.Format
in C# and VB .NET as opposed to the concatenation operators (&
in VB, and +
in C#)?
What is the main difference? Why are everyone so interested in using String.Format
? I am very curious.
Solution 1:
I can see a number of reasons:
Readability
string s = string.Format("Hey, {0} it is the {1}st day of {2}. I feel {3}!", _name, _day, _month, _feeling);
vs:
string s = "Hey," + _name + " it is the " + _day + "st day of " + _month + ". I feel " + feeling + "!";
Format Specifiers (and this includes the fact you can write custom formatters)
string s = string.Format("Invoice number: {0:0000}", _invoiceNum);
vs:
string s = "Invoice Number = " + ("0000" + _invoiceNum).Substr(..... /*can't even be bothered to type it*/)
String Template Persistence
What if I want to store string templates in the database? With string formatting:
_id _translation
1 Welcome {0} to {1}. Today is {2}.
2 You have {0} products in your basket.
3 Thank-you for your order. Your {0} will arrive in {1} working days.
vs:
_id _translation
1 Welcome
2 to
3 . Today is
4 .
5 You have
6 products in your basket.
7 Someone
8 just shoot
9 the developer.
Solution 2:
Besides being a bit easier to read and adding a few more operators, it's also beneficial if your application is internationalized. A lot of times the variables are numbers or key words which will be in a different order for different languages. By using String.Format, your code can remain unchanged while different strings will go into resource files. So, the code would end up being
String.Format(resource.GetString("MyResourceString"), str1, str2, str3);
While your resource strings end up being
English: "blah blah {0} blah blah {1} blah {2}"
Russian: "{0} blet blet blet {2} blet {1}"
Where Russian may have different rules on how things get addressed so the order is different or sentence structure is different.
Solution 3:
First, I find
string s = String.Format(
"Your order {0} will be delivered on {1:yyyy-MM-dd}. Your total cost is {2:C}.",
orderNumber,
orderDeliveryDate,
orderCost
);
far easier to read, write and maintain than
string s = "Your order " +
orderNumber.ToString() +
" will be delivered on " +
orderDeliveryDate.ToString("yyyy-MM-dd") +
"." +
"Your total cost is " +
orderCost.ToString("C") +
".";
Look how much more maintainable the following is
string s = String.Format(
"Year = {0:yyyy}, Month = {0:MM}, Day = {0:dd}",
date
);
over the alternative where you'd have to repeat date
three times.
Second, the format specifiers that String.Format
provides give you great flexibility over the output of the string in a way that is easier to read, write and maintain than just using plain old concatenation. Additionally, it's easier to get culture concerns right with String.Format
.
Third, when performance does matter, String.Format
will outperform concatenation. Behind the scenes it uses a StringBuilder
and avoids the Schlemiel the Painter problem.
Solution 4:
Several reasons:
-
String.Format()
is very powerful. You can use simple format indicators (like fixed width, currency, character lengths, etc) right in the format string. You can even create your own format providers for things like expanding enums, mapping specific inputs to much more complicated outputs, or localization. - You can do some powerful things by putting format strings in configuration files.
-
String.Format()
is often faster, as it uses aStringBuilder
and an efficient state machine behind the scenes, whereas string concatenation in .Net is relatively slow. For small strings the difference is negligible, but it can be noticable as the size of the string and number of substituted values increases. -
String.Format()
is actually more familiar to many programmers, especially those coming from backgrounds that use variants of the old Cprintf()
function.
Finally, don't forget StringBuilder.AppendFormat()
. String.Format()
actually uses this method behind the scenes*, and going to the StringBuilder
directly can give you a kind of hybrid approach: explicitly use .Append()
(analogous to concatenation) for some parts of a large string, and use .AppendFormat()
in others.
* [edit] Original answer is now 8 years old, and I've since seen an indication this may have changed when string interpolation was added to .Net. However, I haven't gone back to the reference source to verify the change yet.
Solution 5:
String.Format
adds many options in addition to the concatenation operators, including the ability to specify the specific format of each item added into the string.
For details on what is possible, I'd recommend reading the section on MSDN titled Composite Formatting. It explains the advantage of String.Format
(as well as xxx.WriteLine
and other methods that support composite formatting) over normal concatenation operators.