Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 814
williamsandonz Avatar asked Mar 30 '13 22:03

williamsandonz


People also ask

How do you escape a formatted string?

To escape % , you will need to double it up: %% .

How do I format a string in Ruby?

In Ruby we apply the string format syntax (the "%" operator) to ease this creation of formatted string data. After the "%" we specify arguments. A first example. We use the percentage sign ("%") to specify a format code within a string.

How do you escape a string in C#?

"; C# includes escaping character \ (backslash) before these special characters to include in a string. Use backslash \ before double quotes and some special characters such as \,\n,\r,\t, etc. to include it in a string.


1 Answers

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?)

like image 196
Michael Liu Avatar answered Oct 13 '22 22:10

Michael Liu