c# string formatting - c#

I m curious why would i use string formatting while i can use concatenation such as
Console.WriteLine("Hello {0} !", name);
Console.WriteLine("Hello "+ name + " !");
Why to prefer the first one over second?

You picked too simple of an example.
String formatting:
allows you to use the same variable multiple times: ("{0} + {0} = {1}", x, 2*x)
automatically calls ToString on its arguments: ("{0}: {1}", someKeyObj, someValueObj)
allows you to specify formatting: ("The value will be {0:3N} (or {1:P}) on {2:MMMM yyyy gg}", x, y, theDate)
allows you to set padding easily: (">{0,3}<", "hi"); // ">hi <"

You can trade the string for a dynamic string later.
For example:
// In a land far, far away
string nameFormat = "{0} {1}";
// In our function
string firstName = "John";
string lastName = "Doe";
Console.WriteLine(nameFormat, firstName, lastName);
Here, you can change nameFormat to e.g. "{1}, {0}" and you don't need to change any of your other code. With concatination, you would need to edit your code or possibly duplicate your code to handle both cases.
This is useful in localization/internationalization.

There isn't a singular correct answer to this question. There are a few issues you want to address:
Performance
The performance differences in your examples (and in real apps) are minimal. If you start writing MANY concatenations, you will gradually see better memory performance with the formatted string. Refer to Ben's answer
Readability
You will be better off with a formatted string when you have formatting, or have many different variables to stringify:
string formatString = "Hello {0}, today is {1:yyyy-MM-dd}";
Console.WriteLine(formatString, userName, Date.Today);
Extensibility
Your situation will determine what's best. You tell me which is better when you need to add an item between Username and Time in the log:
Console.WriteLine(
#"Error!
Username: " + userName + "
Time: " + time.ToString("HH:mm:ss") + "
Function: " + functionName + "
Action: " + actionName + "
status: " + status + "
---");
or
Console.WriteLine(#"Error!
Username: {0}
Time: {1}
Function: {2}
Action: {3}
status: {4}
---",
username, time.ToString("HH:mm:ss"), functionName, actionName, status);
Conclusion
I would choose the formatted string most of the time... But I wouldn't hesitate at all to use concatenation when it was easier.

I think the main thing here is readability. So I always choose for what have the best readability for each case.
Note:
With string interpolation of C# 6 your code could be simplified to this:
Console.WriteLine($"Hello {name}!");
Which I think better than your two suggested options.

String formatting allows you to keep the format string separate, and use it where it's needed properly without having to worry about concatenation.
string greeting = "Hello {0}!";
Console.WriteLine(greeting, name);
As for why you would use it in the exact example you gave... force of habit, really.

I think a good example is about i18n and l10n
If you have to change a string between different languages, this: "bla "+variable+"bla bla.."
Will give problems to a program used to create sobstitution for your strings if you use a different language
while in this way: "bla {0} blabla" is easily convertible (you will get {0} as part of the string)

Formatting is usually preferred for most of the reasons explained by other members here. There are couple more reasons I want to throw in from my short programming experience:
Formatting will help in generating Culture aware strings.
Formatting is more performant than concatenation. Remember, every concatenation operation will involve creation of temporary intermediate strings. If you have a bunch of strings you need to concatenate, you are better off using String.Join or StringBuilder.

You are using a trivial example where there is not much of a difference. However, if you have a long string with many parameters it is much nicer to be able to use a format string instead of many, many + signs and line breaks.
It also allows you to format numerical data as you wish, i.e., currency, dates, etc.

The first format is recommended. It allows you to specify specific formats for other types, like displaying a hex value, or displaying some specific string format.
e.g.
string displayInHex = String.Format("{0,10:X}", value); // to display in hex
It is also more consistent. You can use the same convention to display your Debug statement.
e.g.
Debug.WriteLine (String.Format("{0,10:X}", value));
Last but not least, it helps in the localisation of your program.

In addition to reasons like Ignacio's, many (including me) find String.Format-based code much easier to read and alter.
string x = String.Format(
"This file was last modified on {0} at {1} by {2}, and was last backed up {3}."
date, time, user, backupDate);
vs.
string x = "This file was last modified on " + date + " at "
+ time + " by " + user + " and was last backed up " + backupDate + ".";

I have found the former approach (using string.format) very useful when overriding ToString() methods in Entity classes.
For example, in my Product class;
public override string ToString()
{
return string.format("{0} : {1} ({2} / {3} / {4}",
this.id,
this.description,
this.brand,
this.model);
}
Then, when users decide they want the product to appear differently it's easy to change the order/contents or layout of the string that is returned.
Of course you still concatentate this string together but I feel string.Format makes the whole thing a bit more readable and maintainable.
I guess that's the short answer I'm giving then isn't it - readability and maintainability for lengthy or complex strings.

Since C# 6 release a few years ago, it is also possible to perform string interpolation. Per example this:
var n = 12.5847;
System.Console.WriteLine(string.Format("Hello World! {0:C2}", n));
Becomes that:
var n = 12.5847;
System.Console.WriteLine($"Hello World! {n:C2}");
And both give you this result:
Hello World! £12.58

Related

String.Format certain argument

I would assume this is a common question but I couldn't seem to find anything on SO or google.
Is it possible to only format a single argument. For example, format string foo = "{0} is {1} when {2}"; so that it returns read "{0} is cray when {2}"?
Intentions:
I am trying to format the string while overriding a method before it gets formatted in it's base method
Success
Got it thanks to this answer, all answers were helpful :).
This unit test worked:
string foo = String.Format("{0} is {1} when {2}", "{0}", "cray", "{2}");
Assert.AreEqual("{0} is cray when {2}", foo);
string bar = string.Format(foo, "this", null, "it works");
Assert.AreEqual("this is cray when it works", bar);
Taking your question at face value, I suppose you could do the following:
string foo = String.Format("{0} is {1} when {2}", "{0}", "cray", "{2}");
That is, simply replace each unevaluated format item with itself.
No, this is not possible. String.Format is going to try and replace every bracketed placeholder. If you do not supply the correct number of arguments, an exception will be raised.
I'm not sure why you are trying to do this, but if you want the output to look like that, you'll have to escape the brackets:
var foo = String.Format("{{0}} is {0} when {{1}}", "cray");
// foo is "{0} is cray when {1}"
Perhaps if you tell us what exactly you're trying to do, we'll be able to better help you.

Why doesn't this string format as currency?

I have the following line:
//Send Email
clntMailBody = clntMailBody + "Order Total: " + String.Format("{0:C}", strOrderTotal + "\n");
Watch shows:
String.Format("{0:C}", strOrderTotal + "\n") "35\n" string
But it only outputs "35". I expected "$35.00" Why is this not working as intended?
Thanks
I'm guessing strOrderTotal is a string? I think {0:C} only works for decimal or int types.
I can't believe all of these answers and no one mentioned this, change your code to
clntMailBody = clntMailBody + "Order Total: " + String.Format("{0:C}", strOrderTotal) + "\n";
And see if that solves your problem, however a better way to do it would be
clntMailBody = String.Format("{0}Order Total: {1:C}\n", clntMailBody, strOrderTotal);
It is much easier to see what is going on and removes a lot of your string concatenation.
If you are willing to do some more re-writing a even better solution is: (I made some logic up to show my example)
StringBuilder clntMailBody = new StringBuilder();
clntMailBody.AppendLine("Some Fixed body Text")
foreach(string lineItem in Invoice)
{
clntMailBody.AppendLine(lineItem);
}
clntMailBody.AppendFormat("Order Total {0:C}", strOrderTotal).AppendLine();
return clntMailBody.ToString();
You haven't shown the declaration of strOrderTotal but by it's name I assuming it's already a string. As it's already a string the formatting won't work.
If you want the formatting to work you'll need to pass the order total in as a number - preferably a decimal.
Because it is a string.
Trying to format a string returns... the string.
You need a numeric value in order to get it formatted as currency.
You will see that the C format specifier is defined in the Standard Numeric Format String page on MSDN. Numeric, not "strings".
I presume that strOrderTotal is string ? It had to be decimal, or double etc
I'm going to assume that strOrderTotal is a string. You should use a numeric type, like double or Decimal.
It looks like (based on the variable name strOrderTotal) that your total is already a string. The "C" format specifier converts a number to currency format, not something that's already a string.
Therefore you need to either manually format your string as currency or apply the currency format when the order total is originally converted to a string (when it's stored in strOrderTotal).
If strOrderTotal is a string you can use this code to format it for currency
clntMailBody = clntMailBody + "Order Total: " + String.Format("{0:C}", decimal.Parse(strOrderTotal) + "\n");
To display a string in the currency format:
StringBuilder sb= new StringBuilder("Your total amount is ");
sb.AppendFormat("{0:C} ", 25 );
Console.WriteLine(sb);
Output:
Your total amount is $25.00

Regex vs Tryparse what is the best in performance

In my ASP.net project I need to validate some basic data types for user inputs. The data types are like numeric, decimal, datetime etc.
What is the best approach that I should have taken in terms of performance? Is it to do it by Regex.IsMatch() or by TryParse()?
Thanks in advance.
TryParse and Regex.IsMatch are used for two fundamentally different things. Regex.IsMatch tells you if the string in question matches some particular pattern. It returns a yes/no answer. TryParse actually converts the value if possible, and tells you whether it succeeded.
Unless you're very careful in crafting the regular expression, Regex.IsMatch can return true when TryParse will return false. For example, consider the simple case of parsing a byte. With TryParse you have:
byte b;
bool isGood = byte.TryParse(myString, out b);
If the value in myString is between 0 and 255, TryParse will return true.
Now, let's try with Regex.IsMatch. Let's see, what should that regular expression be? We can't just say #"\d+" or even #\d{1,3}". Specifying the format becomes a very difficult job. You have to handle leading 0s, leading and trailing white space, and allow 255 but not 256.
And that's just for parsing a 3-digit number. The rules get even more complicated when you're parsing an int or long.
Regular expressions are great for determining form. They suck when it comes to determining value. Since our standard data types all have limits, determining its value is part of figuring out whether or not the number is valid.
You're better off using TryParse whenever possible, if only to save yourself the headache of trying to come up with a reliable regular expression that will do the validation. It's likely (I'd say almost certain) that a particular TryParse for any of the native types will execute faster than the equivalent regular expression.
The above said, I've probably spent more time on this answer than your Web page will spend executing your TryParse or Regex.IsMatch--total throughout its entire life. The time to execute these things is so small in the context of everything else your Web site is doing, any time you spend pondering the problem is wasted.
Use TryParse if you can, because it's easier. Otherwise use Regex.
As other would say, the best way to answer that is to measure it ;)
static void Main(string[] args)
{
List<double> meansFailedTryParse = new List<double>();
List<double> meansFailedRegEx = new List<double>();
List<double> meansSuccessTryParse = new List<double>();
List<double> meansSuccessRegEx = new List<double>();
for (int i = 0; i < 1000; i++)
{
string input = "123abc";
int res;
bool res2;
var sw = Stopwatch.StartNew();
res2 = Int32.TryParse(input, out res);
sw.Stop();
meansFailedTryParse.Add(sw.Elapsed.TotalMilliseconds);
//Console.WriteLine("Result of " + res2 + " try parse :" + sw.Elapsed.TotalMilliseconds);
sw = Stopwatch.StartNew();
res2 = Regex.IsMatch(input, #"^[0-9]*$");
sw.Stop();
meansFailedRegEx.Add(sw.Elapsed.TotalMilliseconds);
//Console.WriteLine("Result of " + res2 + " Regex.IsMatch :" + sw.Elapsed.TotalMilliseconds);
input = "123";
sw = Stopwatch.StartNew();
res2 = Int32.TryParse(input, out res);
sw.Stop();
meansSuccessTryParse.Add(sw.Elapsed.TotalMilliseconds);
//Console.WriteLine("Result of " + res2 + " try parse :" + sw.Elapsed.TotalMilliseconds);
sw = Stopwatch.StartNew();
res2 = Regex.IsMatch(input, #"^[0-9]*$");
sw.Stop();
meansSuccessRegEx.Add(sw.Elapsed.TotalMilliseconds);
//Console.WriteLine("Result of " + res2 + " Regex.IsMatch :" + sw.Elapsed.TotalMilliseconds);
}
Console.WriteLine("Failed TryParse mean execution time " + meansFailedTryParse.Average());
Console.WriteLine("Failed Regex mean execution time " + meansFailedRegEx.Average());
Console.WriteLine("successful TryParse mean execution time " + meansSuccessTryParse.Average());
Console.WriteLine("successful Regex mean execution time " + meansSuccessRegEx.Average());
}
}
Don't try to make regexes do everything.
Sometimes a simple regex will get you 90% of the way and to make it do everything you need the complexity grows ten times or more.
Then I often find that the simplest solution is to use the regex to check the form and then rely on good old code for the value checking.
Take a date for example, use a regex to check for a match on a date format and then use capturing groups to check the values of the individual values.
I'd guess TryParse is quicker, but more importantly, it's more expressive.
The regular expressions can get pretty ugly when you consider all the valid values for each data type you're using. For example, with DateTime you have to ensure the month is between 1 and 12, and that the day is within the valid range for that particular month.

Formatting my string

How can I format a string like this:
string X = "'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}'",????
I remember I used to be able to put a comma at the end and specify the actual data to assign to {0},{1}, etc.
Any help?
Use string.Format method such as in:
string X = string.Format("'{0}','{1}','{2}'", foo, bar, baz);
An alternative is to use Join, if you have the values in a string array:
string x = "'" + String.Join("','", valueArray) + "'";
(Just wanted to be different from the 89724362 users who will show you how to use String.Format... ;)
String.Format("'{0}', '{1}'", arg0, arg1);
The String.Format method accepts a format string followed by one to many variables that are to be formatted. The format string consists of placeholders, which are essentially locations to place the value of the variables you pass into the function.
Console.WriteLine(String.Format("{0}, {1}, {2}", var1, var2, var3));
Your question is a bit vague, but do you mean:
// declare and set variables val1 and val2 up here somewhere
string X = string.Format("'{0}','{1}'", val1, val2);
Or are you asking for something else?
use string.format, and put individual format specifiers inside the braces, after the number and a colon, as in
string s = string.Format(
" {0:d MMM yyyy} --- {1:000} --- {2:#,##0.0} -- {3:f}",
DateTime.Now, 1, 12345.678, 3e-6);
and, as you can see from the example, you don;t need the single quotes to delineate literals, anything not inside braces will be output literally
are you looking for:
String.Format("String with data {0}, {1} I wish to format", "Foo", "Bar");
would result in
"String with data Foo, Bar I wish to format"
Use string.Format as in
var output = string.Format("'{0}', '{1}'", x, y);

String has how many parameters

Before using String.Format to format a string in C#, I would like to know how many parameters does that string accept?
For eg. if the string was "{0} is not the same as {1}", I would like to know that this string accepts two parameters
For eg. if the string was "{0} is not the same as {1} and {2}", the string accepts 3 parameters
How can I find this efficiently?
String.Format receives a string argument with format value, and an params object[] array, which can deal with an arbitrary large value items.
For every object value, it's .ToString() method will be called to resolve that format pattern
EDIT: Seems I misread your question. If you want to know how many arguments are required to your format, you can discover that by using a regular expression:
string pattern = "{0} {1:00} {{2}}, Failure: {0}{{{1}}}, Failure: {0} ({0})";
int count = Regex.Matches(Regex.Replace(pattern,
#"(\{{2}|\}{2})", ""), // removes escaped curly brackets
#"\{\d+(?:\:?[^}]*)\}").Count; // returns 6
As Benjamin noted in comments, maybe you do need to know number of different references. If you don't using Linq, here you go:
int count = Regex.Matches(Regex.Replace(pattern,
#"(\{{2}|\}{2})", ""), // removes escaped curly brackets
#"\{(\d+)(?:\:?[^}]*)\}").OfType<Match>()
.SelectMany(match => match.Groups.OfType<Group>().Skip(1))
.Select(index => Int32.Parse(index.Value))
.Max() + 1; // returns 2
This also address #280Z28 last problem spotted.
Edit by 280Z28: This will not validate the input, but for any valid input will give the correct answer:
int count2 =
Regex.Matches(
pattern.Replace("{{", string.Empty),
#"\{(\d+)")
.OfType<Match>()
.Select(match => int.Parse(match.Groups[1].Value))
.Union(Enumerable.Repeat(-1, 1))
.Max() + 1;
You'll have to parse through the string and find the highest integer value between the {}'s...then add one.
...or count the number of sets of {}'s.
Either way, it's ugly. I'd be interested to know why you need to be able to figure out this number programatically.
EDIT
As 280Z28 mentioned, you'll have to account for the various idiosyncrasies of what can be included between the {}'s (multiple {}'s, formatting strings, etc.).
I rely on ReSharper to analyze that for me, and it is a pity that Visual Studio does not ship with such a neat feature.

Categories