Search code examples
c#.netdateescapingstring-formatting

How to put unprocessed (escaped) words inside String.Format


I am formatting a date:

str = String.Format("{0:MMM d m:mm"+yearStr+"}", dt);

I want to put the word "at" after the "d", but I don't want the string to format it. I just want the word "at".

How can I achieve this?


Solution

  • You can surround literal strings with quotes, which for longer strings is probably easier and a bit more readable than escaping every character with a backslash:

    str = String.Format("{0:MMM d 'at' m:mm"+yearStr+"}", dt);
    

    See Custom Date and Time Format Strings in MSDN Library (search for "Literal string delimiter").

    (And did you mean h:mm instead of m:mm?)