Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

.net - Escaping single quote in String.Format()

I have been all over the 'tubes and I can't figure this one out. Might be simple.

The following String.Format call:

return dt.ToString("MMM d yy 'at' H:mmm");

Correctly returns this:

Sep 23 08 at 12:57

Now let's say I want to add a single quote before the year, to return this:

Sep 23 '08 at 12:57

Since the single quote is a reserved escape character, how do I escape the single quote to get it to display?

I have tried double, triple, and quad single quotes, with no luck.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can escape it using a backslash which you will have to escape. Either

return dt.ToString(@"MMM d 'yy 'at' H:mmm");

or

return dt.ToString("MMM d \'yy 'at' H:mmm");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...