Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escape for "{" inside C# 6 string interpolation [duplicate]

I am hoping to use a "{" inside a string interpolation statement but I'm having trouble finding the escape character to do it.

var val = "ERROR_STATE";
var str = $"if(inErrorState){ send 1,\"{val}\" }"

Desired output:

if(inErrorState){send 1,"ERROR_STATE"}

The simple solution is to just not use string interpolation, but I think this way of doing it is easier to read.

like image 646
Felix Castor Avatar asked Feb 24 '17 13:02

Felix Castor


People also ask

What is escape sequence in C?

Escape sequences You can represent any member of the execution character set by an escape sequence. They are primarily used to put nonprintable characters in character and string literals. For example, you can use escape sequences to put such characters as tab, carriage return, and backspace into an output stream.

What is the meaning of \n and \r in C?

\n means new line. It means that the cursor must go to the next line. \r means carriage return.

What is the use of \r in C?

When writing to an interactive terminal on stdout or stderr , '\r' can be used to move the cursor back to the beginning of the line, to overwrite it with new contents.

What is the use of \t in C?

\t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line.


1 Answers

Type { twice to escape it:

$"if(inErrorState){{send 1, \"{val}\" }}"

BTW you can do the same with double quotes.

like image 54
Sergey Berezovskiy Avatar answered Oct 14 '22 21:10

Sergey Berezovskiy