What is the cleanest method for adding a $
character in a string literal?
The best solution I've come up with so far is """${"$"}..."""
, which looks ugly to me.
In Interpolated Strings, the dollar sign ($) is used to tell the C# compiler that the string following it is to be interpreted as an Interpolated String. The curly braces encapsulate the values (of the variables) to include in the text. Note: you can include the literal curly braces by using “{{“ and “}}” accordingly.
To Python, those dollar signs mean nothing at all. Just like the 'D' or 'a' that follow, the dollar sign is merely a character in a string. To your source-code control system, the dollar signs indicate a substitution command.
It's used to reference a variable within string: let someVar = "World!" console. log(`Hello ${someVar}`); // Output is Hello World!
Just put \$ and then works.
To escape the dollar sign inside a string literal, use the backslash character:
"\$"
To escape it in a raw string literal ("""..."""
), the workaround you provided is indeed the easiest solution at the moment. There's an issue in the bug tracker, which you can star and/or vote for: KT-2425.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With