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

前端 未结 4 1336
我寻月下人不归
我寻月下人不归 2020-12-30 21:15

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\

4条回答
  •  無奈伤痛
    2020-12-30 21:22

    string.Format(@"{0:MMM d \a\t m:mm" + yearStr + "}", dt);
    

    Note the double escaping - I used a varbatim string so I was able to write \ inside the string as a normal character. The formatting routine for DateTime then interprets this (again) as an escape sequence.

    Here is a simpler variant:

    string.Format("{0:MMM d} at {0:m:mm" + yearStr + "}", dt);
    

    The first variant might be considered disgusting by some. The latter one is very clear to read, though.

提交回复
热议问题