Using string.Format for simple things?

In my early days of .Net programming, I used string.Format () only for complex string concatenations, like compiling strings like

Problem with customer order 234 on 2/2/2002 and payment identifier 55543.

But now I use string.Format for almost every string concatenation I have to perform, as well as simple ones like a string prefix with something.

Console.WriteLine(string.Format("\t\t{0}", myString)); 

Are there any possible overheads? Maybe I should use the regular + operator to perform these simple operations?

What is your opinion on this?

+4
source share
7 answers

For simple string concatenations, use the + approach. This is easier for simple things that do not require a format.

For more complex strings that have a specific format and where it is useful to preserve the structure of the entire string and provide a placeholder for input, use String.Format .

And yes, there is overhead. String.Format uses StringBuilder under covers. Simple string concatenations will be much faster in these scenarios. Several tests and blog posts on this topic can be found quite easily. Of course, it all depends on your use. If there are small ends in the loop, then reusing String.Format is more likely to be more noticeable than a simple + concat. If you create a large chain in a loop, then the classic example is preferable to StringBuilder , and related questions on concat and StringBuilder can be found on SO.

EDIT: To clarify this, this is impractical: String.Format("{0}{1}", a, b) , since there is not much formatting. It is just a + b . Unfortunately, I met such examples in production code, and as soon as I see String.Format, I expect to see something that needs to be structured in a certain way, and not just concat.

OTOH, consider this phone number: "(" + area + ") " + number + " x" + extension - there is too much going on, and this is not easy to change. In this case, String.Format is preferable: String.Format("({0}) {1} x{2}", area, number, extension) . This is still a trivial example, but you get the idea.

+5
source

Simple concatenation is more efficient for simple things. Use String.Format() when things String.Format() complicated and your code is easier to read.

I personally do the same thing (as long as the function I call does not handle formatting for me).


Clarrification

For regular string concatenation, for example, "Hello " + "World!"; , I would use StringBuilder. Your example formats the line for output by adding two tabs ... which I consider rather formatting.

There is a difference between formatting and concatenation ... be careful what you use for.


String.Format() uses StringBuilder internally, so concatenation will be more efficient than doing regular string concatenation.

You might want to change your example, as Console.WriteLine() can handle the formatting itself (no String.Format() ):

 Console.WriteLine("\t\t{0}", myString); 

Strike>

+2
source

I also use string.Format for most operations that need to combine two or more lines / values, since it makes it easier to read code than starting and stopping lines with + in the middle.

To expand

 string value = string.Format("Hello {0}", user.username); 

much more readable and extensible than

 string value = "Hello" + user.username 

for example, if you want to add the last login date as a system update, you can simply expand the code to the next

 string value = string.Format("Hello {0}, you last logged in {1}", user.username, user.lastLogin); 
+2
source

My rule: if I have to use + (concatenation) more than once, I change it to string.Format .

 string a = "Something: " + x; // ok string b = "Something: " + x + " something else"; // change it to a string.Format string bB = string.Format("Something: {0} something else", x); // much nicer to read string c = "Something: " + x + " " + y + " " + z; // Yuck string cC = string.Format("Something: {0} {1} {2}", x, y, x); // Much nicer 
+1
source

I don’t know about performance, someone will most likely provide this data, but I feel that String.Format is the way to go if you want to put the format string in your configuration file (or in the database field or resource file or something else). Thus, if you want to adjust the number of tabs, switch to spaces, add separators or something else, you do not need to recompile.

0
source

I prefer to use String.Concat instead of String.Format when I just need the prefix or suffix of a given string. I prefer to explicitly call the Concat method and then use the + operator, and if you are already using String.Format , it's just a matter of switching one keystroke.

IIRC, the String.Concat method will be the same as using the operator, so it will also be faster than String.Format .

0
source

I almost always use the format, although instead of using the static string method, I use the extension method. It’s easier for me to understand, easier to change, and generally easier to maintain. It can also simplify localization, since it does not introduce ordering issues, as concatenation does.

Honestly, what looks better?

 "You have {0} widgets.".Frmt(widgetCount) "You have " + widgetCount.ToString() + " widgets." 
0
source

Source: https://habr.com/ru/post/1304577/


All Articles