<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx</link><description>A while ago I wrote an article about StringBuilder and a reader mailed me to ask about the efficiency of using String.Format instead. This reminded me of a bone I have to pick with the BCL. Whenever we make a call to String.Format , it has to parse the</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1653756</link><pubDate>Tue, 11 Nov 2008 15:11:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653756</guid><dc:creator>Vincent Croquette</dc:creator><description>&lt;p&gt;&amp;lt;quote&amp;gt;Readability is king unless there are big-Oh reasons to change...&amp;lt;/quote&amp;gt;&lt;/p&gt;
&lt;p&gt;This could also be one of my mottos&lt;/p&gt;
&lt;p&gt;yet, even though str1 + str2 is sometimes more readable than string.Format(... I&amp;#39;ve stopped using it for performance reasons&lt;/p&gt;
&lt;p&gt;Anders Hejlsberg used it in his PDC presentation and I thought it was very approprate in this context (i.e. the context in which someone else than you has to read your code)&lt;/p&gt;
&lt;p&gt;Jon, glad to see you also own an eee. I bought it for ebooks reading... but was surprised I could actually develop WCF services and Silverlight frontends with it (OK, the performance is not optimal, but it&amp;#39;s still usable)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653756" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1653057</link><pubDate>Tue, 04 Nov 2008 19:40:03 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653057</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Sam: Look at the advice you give at the end. It&amp;#39;s about performance, and not about readability:&lt;/p&gt;
&lt;p&gt;&amp;quot;However, When you need to concat 5 or more strings, use multiple statements of 4 strings at once. This is appropriate for when you have a known number of strings.&amp;quot;&lt;/p&gt;
&lt;p&gt;I would always go for the simpler approach until the performance proves to be a bottleneck.&lt;/p&gt;
&lt;p&gt;As for this blog post: it&amp;#39;s about performance without giving advice to readers (seeing as the request I&amp;#39;ve made hasn&amp;#39;t come to fruition yet). However, look at the last bit:&lt;/p&gt;
&lt;p&gt;&amp;quot;I&amp;#39;m not saying it would do a huge amount to aid performance, but it could shave off a little time here and there, as well as making it even more obvious what the format string is used for.&amp;quot;&lt;/p&gt;
&lt;p&gt;See - it&amp;#39;s about making it more readable *as well* as performing better.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653057" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1653053</link><pubDate>Tue, 04 Nov 2008 19:02:58 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653053</guid><dc:creator>Sam</dc:creator><description>&lt;p&gt;I don&amp;#39;t see why you say the dotnetperls.com article is solely focused on performance. It has many more examples than this blog post. Obviously you want to write clear code. &lt;/p&gt;
&lt;p&gt;In my opinion this post is focused solely on performance.&lt;/p&gt;
&lt;p&gt;Sam&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653053" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1653051</link><pubDate>Tue, 04 Nov 2008 18:47:20 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653051</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;The problem with that article is it&amp;#39;s focused solely on the performance. Sure, concatenating 5 strings in one go may be slightly slower than keeping them within blocks of four - but it&amp;#39;s more readable.&lt;/p&gt;
&lt;p&gt;Readability is king unless there are big-Oh reasons to change...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653051" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1653050</link><pubDate>Tue, 04 Nov 2008 18:39:47 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653050</guid><dc:creator>Charles Feduke</dc:creator><description>&lt;p&gt;Working my way through some of your older articles, I thought you may find:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.dotnetperls.com/Content/String-Concat.aspx"&gt;www.dotnetperls.com/.../String-Concat.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;interesting. &amp;nbsp;Well okay the interesting part is where he determines that 4 concats are substantially better than 5, and breaking up your concatenations into groups of 4 is always better than just doing more than 4. &amp;nbsp;At some point you resort to a StringBuilder of course, but still interesting to know.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653050" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1650474</link><pubDate>Fri, 10 Oct 2008 14:35:21 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650474</guid><dc:creator>Pavel Minaev</dc:creator><description>&lt;p&gt;Interestingly, that&amp;#39;s one case where F# ML curring functions allow for smoother syntax. You could curry Format with the format string, and if the implementation is smart, it will parse it at that point (or, alternatively, parse it first time it&amp;#39;s needed, but then memoize it). Then you save the function that is the result of that currying, and use it. I.e.:&lt;/p&gt;
&lt;p&gt;// Core functionality&lt;/p&gt;
&lt;p&gt;type ParsedFormatString = ...&lt;/p&gt;
&lt;p&gt;let ParseFormatString s = ...&lt;/p&gt;
&lt;p&gt;let FormatPreparsed parsedFormatString args = ...&lt;/p&gt;
&lt;p&gt;// BCL-style Format on top of that, with currying and memoization&lt;/p&gt;
&lt;p&gt;let Format formatString = FormatPreparsed (ParseFormatString formatString)&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;// Parse as usual&lt;/p&gt;
&lt;p&gt;for x = 1 to 10 do&lt;/p&gt;
&lt;p&gt; &amp;nbsp;for y = 1 to 10 do&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Format &amp;quot;{0} {1}&amp;quot; x y&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;// Memoize and reuse&lt;/p&gt;
&lt;p&gt;let FormatPair = Format &amp;quot;{0} {1}&amp;quot;&lt;/p&gt;
&lt;p&gt;for x = 1 to 10 do&lt;/p&gt;
&lt;p&gt; &amp;nbsp;for y = 1 to 10 do&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;FormatPair x y&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650474" width="1" height="1"&gt;</description></item><item><title>Qué es más rapido: String.Format o String.Concat?</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1650351</link><pubDate>Thu, 09 Oct 2008 17:28:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650351</guid><dc:creator>.NET a 2.860 metros de altura</dc:creator><description>&lt;p&gt;Esta es una pregunta que he tenido pendiente mucho tiempo. En general yo prefiero usar String.Format&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650351" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1650332</link><pubDate>Thu, 09 Oct 2008 15:15:08 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650332</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Richard: I certainly wouldn&amp;#39;t suggest doing that. But I&amp;#39;d suggest doing:&lt;/p&gt;
&lt;p&gt;sb.Append(x);&lt;/p&gt;
&lt;p&gt;sb.Append(&amp;quot;:&amp;quot;);&lt;/p&gt;
&lt;p&gt;sb.Append(y);&lt;/p&gt;
&lt;p&gt;instead of&lt;/p&gt;
&lt;p&gt;sb.AppendFormat(&amp;quot;{0}:{1}&amp;quot;, x, y);&lt;/p&gt;
&lt;p&gt;(If performance were an issue, of course.)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650332" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1650331</link><pubDate>Thu, 09 Oct 2008 15:06:43 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650331</guid><dc:creator>Richard</dc:creator><description>&lt;p&gt;As for the relative efficiency of String.Format versus StringBuilder.AppendFormat, have a look at the code for the String.Format(IFormatProvider, string, params object[]) method:&lt;/p&gt;
&lt;p&gt;public static string Format(...)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StringBuilder builder = new StringBuilder(format.Length + (args.Length * 8));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;builder.AppendFormat(provider, format, args);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return builder.ToString();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Clearly, if you already have a StringBuilder, it&amp;#39;s more efficient to call AppendFormat than Append(String.Format). &lt;/p&gt;
&lt;p&gt;The only caveat is if the format string is invalid: AppendFormat will append the start of the format string, and then throw an exception, whereas Append(String.Format) will throw an exception without modifying the StringBuilder instance.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650331" width="1" height="1"&gt;</description></item><item><title>Interesting Finds: 2008.10.08~2008.10.09</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1650228</link><pubDate>Thu, 09 Oct 2008 00:52:59 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650228</guid><dc:creator>gOODiDEA.NET</dc:creator><description>&lt;p&gt;.NET Richmond Code Camp 2008.2 - Functional C# Recap ASP.NET MVC with NHaml - F# Edition Formatting strings&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650228" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1649996</link><pubDate>Tue, 07 Oct 2008 13:25:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649996</guid><dc:creator>ffpf</dc:creator><description>&lt;p&gt;I would like to see the format string compiled like Regex can be; possibly broken into an expression tree so that it only needs to get parsed once and is thereafter faster to run. I&amp;#39;ve often wondered why we don&amp;#39;t have the concept of a compiled format string so that calls like string.Format will be more efficient in repeated calls.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649996" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1649970</link><pubDate>Tue, 07 Oct 2008 08:57:55 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649970</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@James: Gosh, that&amp;#39;s an interesting idea. I like that. I think it would require some extra support above and beyond what the current interfaces express, but it&amp;#39;s still a neat plan.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649970" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1649969</link><pubDate>Tue, 07 Oct 2008 08:45:19 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649969</guid><dc:creator>James Hart</dc:creator><description>&lt;p&gt;How about strongly-typing compiled format strings as delegates, in the manner of F#? Maybe not going so far as to have the compiler actually parse the format string and infer the types, but how about:&lt;/p&gt;
&lt;p&gt;var format = Formatter.Compile&amp;lt;string, double&amp;gt;(&amp;quot;{0} ({1:0.00})&amp;quot;);&lt;/p&gt;
&lt;p&gt;string formatted = format(&amp;quot;Hello&amp;quot;, Math.Pi); // &amp;quot;Hello (3.14)&amp;quot;&lt;/p&gt;
&lt;p&gt;Gives you the opportunity to fail at Formatter.Compile if the format string tries to use a format item that isn&amp;#39;t specified or with an incompatible format string for a given type.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649969" width="1" height="1"&gt;</description></item><item><title>Reflective Perspective - Chris Alcock  &amp;raquo; The Morning Brew #195</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1649962</link><pubDate>Tue, 07 Oct 2008 07:04:14 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649962</guid><dc:creator>Reflective Perspective - Chris Alcock  » The Morning Brew #195</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Reflective Perspective - Chris Alcock &amp;nbsp;&amp;raquo; The Morning Brew #195&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649962" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1649901</link><pubDate>Mon, 06 Oct 2008 18:55:59 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649901</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@David: No, I haven&amp;#39;t. Will do :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649901" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1649900</link><pubDate>Mon, 06 Oct 2008 18:40:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649900</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;As I suspected, it was the blog engine. &amp;lt;sigh&amp;gt;&lt;/p&gt;
&lt;p&gt;Oh, and I&amp;#39;m looking forward to the DNR show tomorrow too. It&amp;#39;ll be interesting to see if they&amp;#39;ve managed to edit my drivel into something which makes me sound intelligent!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649900" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1649899</link><pubDate>Mon, 06 Oct 2008 18:38:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649899</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;It certainly makes sense to me. The Regex class has static methods for performing regular expression matching (similar to String.Format), but you can also create an instance of the Regex class with your regular expression and the options you want to use, which can be optimized for reuse. I don&amp;#39;t see any reason not to have something similar for String.Format.&lt;/p&gt;
&lt;p&gt;Have you made this suggestion to the BCL team directly?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649899" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1649895</link><pubDate>Mon, 06 Oct 2008 18:31:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649895</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;That&amp;#39;s very strange. It should, of course, be &amp;quot;ret = (char) (&amp;#39;A&amp;#39; + rng.Next(26));&amp;quot;. I&amp;#39;ll fix it in a minute. I wonder how that got past the formatter though. Very strange. It could be yet another case of the blog engine &amp;quot;fixing&amp;quot; things for me :(&lt;/p&gt;
&lt;p&gt;The performance issue is a general one though - you can always rewrite a call to String.Format as a (potentially large) call to Concat, but that&amp;#39;s not as readable. It would be nice if the framework provided a way to have our cake and eat it - in the form of a &amp;quot;parse once, use often&amp;quot; object.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649895" width="1" height="1"&gt;</description></item><item><title>re: Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#1649890</link><pubDate>Mon, 06 Oct 2008 17:26:20 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649890</guid><dc:creator>Kalpesh</dc:creator><description>&lt;p&gt;Surprisingly, the code doesn&amp;#39;t compile.&lt;/p&gt;
&lt;p&gt;Line: ret = (char) (&amp;#39;A&amp;#39; + rng.Next(26));&lt;/p&gt;
&lt;p&gt;Cannot implicitly convert type &amp;#39;char&amp;#39; to &amp;#39;char[]&amp;#39;&lt;/p&gt;
&lt;p&gt;The point here is - one should use Concat against Format (in this case). The format will be useful where it does some real formatting operation (e.g. {0:c}&lt;/p&gt;
&lt;p&gt;BTW, I am waiting to hear your DNR show ;)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649890" width="1" height="1"&gt;</description></item></channel></rss>