Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape string interpolation in snippet

String interpolation is great and snippets are great, but they don't play nice together. I have a snippet that looks (in part) like this:

job.Location = $"{e["$locationfield$"]}";
return true;

The locationfield part is supposed to be the replacement, but of course it will see as '"{e[" being a replacement. So when you try to use the snippet, that part is messed up:

job.Location = locationfield

And the rest of the code following is gone (because it can't match another $ presumably).

Is there a way around that? Or can you just not use string interpolation in your snippets?

like image 293
Matt Burland Avatar asked Aug 30 '16 20:08

Matt Burland


People also ask

How to use string interpolation in C#?

Structure of an interpolated string. To identify a string literal as an interpolated string, prepend it with the $ symbol. You can't have any white space between the $ and the " that starts a string literal. To concatenate multiple interpolated strings, add the $ special character to each string literal.

Why use string interpolation?

String Interpolation is a process in which the placeholder characters are replaced with the variables (or strings in this case) which allow to dynamically or efficiently print out text output. String Interpolation makes the code more compact and avoids repetition of using variables to print the output.


1 Answers

Ok, as it turns out, it's dead simple. I did this on a whim and it worked:

job.Location = $$"{e["$locationfield$"]}";
return true;

Another solution that I didn't try, is that you can actually specify what the snippet should use as a delimiter:

<Code Language="csharp" Delimiter="$">

Just change the $ to something else you aren't using in this particular block of code.

like image 156
Matt Burland Avatar answered Sep 29 '22 17:09

Matt Burland