0

I am trying add quotes in between eventTime variable. But getting compile time error for both code statements below.

Code:

string message  = string.Format("Your event # {0}, is started at "{1}" in Texas.", eventId, eventTime);

string message  = $"Your event # {eventId}, is started at " + {eventTime} + "  in Texas.";

Expected Output:

Your event # 1, is started at "2" in Texas.

This may be simple, But I am not able to find proper syntax for this.

Any help would be Great.

Note:

Because of down votes , I know I have to escape double quotes so I followed this post and tried to escape , But it failed. So I posted the question to know proper syntax. Thanks to all , this helped me to learn , so below is my answer:

Using verbatim string literal.

string message  = $@"Your event # {eventId}, is started at ""{eventTime}""  in Texas.";

Hope helps someone.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Shaiju T
  • 6,201
  • 20
  • 104
  • 196
  • You're missing the backslash escape before the quotes. – Matthew Watson Oct 04 '19 at 11:01
  • This isn't a string interpolation issue. All you need to do is to escape the quotes within the string. – ProgrammingLlama Oct 04 '19 at 11:01
  • $@"Your event # {eventId}, is started at ""{eventTime}"" in Texas." – phuzi Oct 04 '19 at 11:02
  • 2
    Why are people downvoting this question? It may be a duplicate, but the question is clear. Questions [shouldn't be downvoted because they are duplicates](https://meta.stackexchange.com/questions/62819/should-we-downvote-duplicates), and they certainly shouldn't be downvoted just because you think the OP should know the answer... – Matthew Watson Oct 04 '19 at 11:06
  • 1
    @John , Thanks for the advice, But I think this question is partially duplicate , I know about escaping the double quotes but didn't know were to add it correctly. By the way I found a solution myself and added as answer in the question because I cant add it as answer. – Shaiju T Oct 04 '19 at 11:37
  • When escaping quotes with the backslash, it includes the backslash in the output. Any idea why? – Tigerware Sep 03 '21 at 20:19

2 Answers2

2

Use the escape character \ like this:

string message = $"Your event # {eventId}, is started at \"{eventTime}\" in Texas.";

message then contains

Your event # 1, is started at "2" in Texas.

given that eventId == 1 and eventTime == 2.

The escape sequence \" marks the quoatation marks part of the string, thus not terminating it.

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • 1
    Thanks you, Appreciate ! , I saw this but didint use this becuase i was after [verbatim string literal](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim). I have added the solution to my question it self because i cant add it as answer because someone has marked as duplicate. – Shaiju T Oct 04 '19 at 11:31
  • 1
    @BluE not sure what you mean... "output" as in console output? or are you looking at the string in the VS debugger? See [this](https://sharplab.io/#gist:a0ce900657123125315dee695d8f185d) for an example of the correct output. – Thomas Flinkow Sep 05 '21 at 15:24
  • @ThomasFlinkow I was referring to the Rider immediate window, where it printed different (wrong) output apparently. – Tigerware Sep 05 '21 at 15:51
0

You need the escape characters Please follow this link https://devblogs.microsoft.com/csharpfaq/what-character-escape-sequences-are-available/

string message  = string.Format("Your event # {0}, is started at \"{1}\" in Texas.", eventId, eventTime);
Ihtsham Minhas
  • 1,415
  • 1
  • 19
  • 31