If you are going to take it so literally, you may want to give 'em a quote or two

by Michael S. Kaplan, published on 2007/10/09 10:31 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2007/10/09/5375024.aspx

NOTE: In addition to the wonderful lesson in this post, please keep in mind the lesson from Dumb quotes... or maybe they are just smart-ass quotes when you are applying the solution discussed below....

The other day, Michel asked me via email:

Hi Michael,

Would you know how I can escape the "h" character in order to display it without being interpreted?

To get for instance a more natural "9 h 58" rather than electronic clock-like "09:58" for French?

This is the natural format .... for French content, but I find the time is used so frequently in software sentences that it is less shocking displayed as "Hhmm".

I could not find this information in the help. I tried backslashing it, to no avail.


I too had real problems trying to find the information in help or documentation or other resources. The question is not just how to handle the situation of ambiguous literals in a format string; even unambiguous literals are disallowed, as my attempt to add an "ABC " prefix to the long date format below:

The trick here is to always quote literals with single quotes, like this:

That was easy, right? You can then even see it show up in the user interface:

Looks like I should update my Defender signatures, now that I look a little closer at the tray, too. :-)

Okay, let's go back to Michel's original question and apply the same solution:

There we go. Just what the doctor ordered.

Note that this does not apply in the .NET Framework, which can run the following code with no problem, testing both ways:

CultureInfo ci = new CultureInfo("en-US");
DateTimeFormatInfo dtfi = ci.DateTimeFormat;
dtfi.LongDatePattern = "'QQQQ' " + dtfi.LongDatePattern;
Console.WriteLine(DateTime.Now.ToString("D", dtfi));
dtfi.LongDatePattern = "RRRR " + dtfi.LongDatePattern;
Console.WriteLine(DateTime.Now.ToString("D", dtfi));

and it will return:

QQQQ Monday, October 08, 2007
RRRR QQQQ Monday, October 08, 2007 

Now for the record, the rule in unmanaged code is not always applicable, 100% of the time. Clearly there are no problems with the punctuation in the format string, for example.

And people running in managed code might think they don't need to bother with the quoting, but you never know when you will have a component that does a bit of unmanaged work as well, so the quotes that won't ever hurt and may save you from unintended failure.

So to be safe, it is always better to quote literals because you never know when the code that parses and/or validates the format string will (in managed or unmanaged environments) try to take your string at its word, a bit too literally!


This post brought to you by ' (U+0027, a.k.a. APOSTROPHE)

Ben Cooke on 9 Oct 2007 1:03 PM:


Don't call it "the tray", or Raymond might get upset!


Björn on 10 Oct 2007 3:01 PM:

Yeah, you do not want Raymond to call you wrong [1], do you? :]

[1] http://blogs.msdn.com/oldnewthing/archive/2003/09/10/54831.aspx

referenced by

2013/04/03 “The ‘Smart Quotes’ are leaking all over the Internet!”

go to newer or older post, or back to index or month or day