string-interpolation

How compiler handles string interpolation

天涯浪子 提交于 2019-12-20 04:30:50
问题 I am using string interpolation for a method attribute like - const string User = "SomeUser"; const string Admin = "Admin"; . . . [Authorize(Roles = $"{User},{Admin}")] public IHttpActionResult Get() But Visual Studio gives an error - An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type When I try "+" for string concatenation it works [Authorize(Roles = User + "," + Admin)] public IHttpActionResult Get() Even if I

Why can't I use the conditional operator in an interpolated string without brackets? [duplicate]

放肆的年华 提交于 2019-12-20 03:27:13
问题 This question already has an answer here : How to use the ternary operator inside an interpolated string? (1 answer) Closed 2 years ago . Why can't I use the inline conditional-operator inside a c#-6 string interpolation, without encompassing it within brackets? and the errors: As you can see, it appears the parser is having a hard time. Is this a bug, or a feature of the string interpolation mechanism? 回答1: From MSDN (emphasis mine): $"{person.Name, 20} is {person.Age:D3} year {(p.Age == 1 ?

Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater.

最后都变了- 提交于 2019-12-19 12:52:47
问题 The following line does not compile when I put in a Razor View. var extPropLookupNameCompania = $"extension_{SettingsHelper.ClientId.Replace("-", "")}_{"Compania"}"; However in the controller the same line works perfectly fine. Why I cant user string interpolation on the razor views? or Maybe I need to configure something? 回答1: If you are experiencing this error in a .NET Framework 4.5.1 project, upgrading to 4.5.2 solves the problem. 回答2: You have to encapsulate it with braces like this:

How to Create Text Files from an Array of Values in Powershell

丶灬走出姿态 提交于 2019-12-19 10:45:09
问题 I have a text file "list.txt" with a list of hundreds of URL's that I want to parse, along with some common-to-all config data, into individual xml files (config files) using each value in "list.txt", like so: list.txt contains: line_1 line_2 line_3 The boilerplate config data looks like (using line_1 as an example): <?xml version="1.0"?> <Website xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Url>line_1.mydomain.com</Url> <Title>line_1<

PHP interpolating string for function output

亡梦爱人 提交于 2019-12-19 05:11:20
问题 PHP supports variable interpolation in double quoted strings, for example, $s = "foo $bar"; But is it possible to interpolate function call results in the double quoted string? For example, $s = "foo {bar()}"; Something like that? It doesn't seem not possible, right? 回答1: The double quote feature in PHP does not evaluate PHP code, it simply replaces variables with their values. If you want to actually evaluate PHP code dynamically (very dangerous), you should use eval : eval( "function foo()

Why can't _ be used inside of string interpolation?

牧云@^-^@ 提交于 2019-12-18 04:34:15
问题 This works (x => s"$x") but this (s"${_}") results in [error] ...: unbound placeholder parameter [error] (s"${_}") Is this a case of leaky abstraction? Furthermore: (s"$_") fails with a completely different output: [error] ...: invalid string interpolation: `$$', `$'ident or `$'BlockExpr expected [error] (s"$_") [error] ^ [error] ...: unclosed string literal [error] (s"$_") 回答1: Calling string interpolation a leaky abstraction is totally right in my opinion. While it works fine in most cases,

Angular - Interpolate string with html

怎甘沉沦 提交于 2019-12-18 02:51:12
问题 So i've got a "template string" that looks like this: var templateString = "Hello my name is {{name}}"; The name that I want to interpolate is a in variable. So I proceeded this way: var miniScope = { name: "Chuck" }; var sentence = $interpolate(templateString)(miniScope); /* sentence: "Hello my name is Chuck" */ This works. Now I'd like to bold the name. I've obviously tried: var miniScope = { name: "<strong>Chuck</strong>" }; But the html code gets escaped. Any idea how I can achieve this?

Patternlab / Twig Variable Interpolation doesn't work with string from json

若如初见. 提交于 2019-12-17 21:30:42
问题 I'm working on a project that's based on the twig patternlab framework. I'm using JSON files for most of my content, especially for pages. I want to integrate a link (build by an atom) into a text I got from my JSON file that has a placeholder for the link. I'm working with texts from a multilingual cms so putting a placeholder into the text content is the easiest way to keep it flexible. This is an excerpt from my json file { "legal" : "Mit dem Absenden des Formulars akzeptieren Sie unsere #

Escape a dollar sign in string interpolation

北战南征 提交于 2019-12-17 17:53:08
问题 How do I escape a dollar sign in string interpolation? def getCompanion(name: String) = Class.forName(s"my.package.$name\$") // --> "error: unclosed string literal" 回答1: Just double it scala> val name = "foo" name: String = foo scala> s"my.package.$name$$" res0: String = my.package.foo$ 来源: https://stackoverflow.com/questions/16875530/escape-a-dollar-sign-in-string-interpolation

Can Perl string interpolation perform any expression evaluation?

独自空忆成欢 提交于 2019-12-17 17:39:23
问题 related to question: How do I substitute with an evaluated expression in Perl? In Perl, is there a way like in Ruby to do: $a = 1; print "#{$a + 1}"; and it can print out 2 ? 回答1: There's a similar shorthand in Perl for this: $a = 1; print "@{[$a + 1]}" This works because the [] creates a reference to an array containing one element (the result of the calculation), and then the @{} dereferences the array, which inside string interpolation prints each element of the array in sequence. Since