string-interpolation

String interpolation in a multi-line Groovy string

喜你入骨 提交于 2021-01-29 10:31:01
问题 I have a multi-line string in which I dynamically populate an SQL query from a map. I am getting a MissingPropertyException that is a result of the query not recognising the map key or values. Is there a way around this? def multiString = """ def person = ['John': 'Builder'] person.each{ key, value -> String query = """ UPDATE person SET value = '${value}' WHERE name = '${key}' """ } """ 回答1: I figured it out. I needed to escape the $ using a backslash. def multiString = """ def person = [

Avoidable boxing in string interpolation

China☆狼群 提交于 2021-01-27 07:29:12
问题 Using string interpolation makes my string format looks much more clear, however I have to add .ToString() calls if my data is a value type. class Person { public string Name { get; set; } public int Age { get; set; } } var person = new Person { Name = "Tom", Age = 10 }; var displayText = $"Name: {person.Name}, Age: {person.Age.ToString()}"; The .ToString() makes the format longer and uglier. I tried to get rid of it, but string.Format is a built-in static method and I can't inject it. Do you

Avoidable boxing in string interpolation

我与影子孤独终老i 提交于 2021-01-27 07:28:00
问题 Using string interpolation makes my string format looks much more clear, however I have to add .ToString() calls if my data is a value type. class Person { public string Name { get; set; } public int Age { get; set; } } var person = new Person { Name = "Tom", Age = 10 }; var displayText = $"Name: {person.Name}, Age: {person.Age.ToString()}"; The .ToString() makes the format longer and uglier. I tried to get rid of it, but string.Format is a built-in static method and I can't inject it. Do you

Avoidable boxing in string interpolation

青春壹個敷衍的年華 提交于 2021-01-27 07:25:47
问题 Using string interpolation makes my string format looks much more clear, however I have to add .ToString() calls if my data is a value type. class Person { public string Name { get; set; } public int Age { get; set; } } var person = new Person { Name = "Tom", Age = 10 }; var displayText = $"Name: {person.Name}, Age: {person.Age.ToString()}"; The .ToString() makes the format longer and uglier. I tried to get rid of it, but string.Format is a built-in static method and I can't inject it. Do you

String interpolation inside string interpolation

依然范特西╮ 提交于 2021-01-20 12:31:09
问题 Is it possible to have a variable with a string format that you would like interpolated. public class Setting { public string Format { get; set; } } var setting = new Setting { Format = "The car is {colour}" }; var colour = "black"; var output = $"{setting.Format}"; Expected output "The car is black". 回答1: You can't do that. String interpolation is a purely compile-time feature. 回答2: No you can't do that, but you can achieve the same with a slightly different approach, that I've come to like:

Does C# 6 string interpolation use boxing like string.Format() does for its arguments?

那年仲夏 提交于 2020-12-05 07:04:26
问题 I am asking this for performance sake - using lots of boxing makes lots of heap allocations which brings more GC collects which sometimes causes apps to freeze for a glimpse which annoy users. 回答1: All string interpolation does (at least in the common case) is to call string.Format() . Right now, calling string.Format() allocates quite a lot and not just due to boxing (for example, string.Format("{0:s} - {1:B}: The value is: {2:C2}", DateTime.UtcNow, Guid.NewGuid(), 3.50m) makes 13

Using expanding strings as Powershell function parameters

萝らか妹 提交于 2020-08-02 17:52:50
问题 I'm trying to write a function that will print a user-supplied greeting addressed to a user-supplied name. I want to use expanding strings the way I can in this code block: $Name = "World" $Greeting = "Hello, $Name!" $Greeting Which successfully prints Hello, World! . However, when I try to pass these strings as parameters to a function like so, function HelloWorld { Param ($Greeting, $Name) $Greeting } HelloWorld("Hello, $Name!", "World") I get the output Hello, ! World Upon investigation,

Using expanding strings as Powershell function parameters

情到浓时终转凉″ 提交于 2020-08-02 17:49:17
问题 I'm trying to write a function that will print a user-supplied greeting addressed to a user-supplied name. I want to use expanding strings the way I can in this code block: $Name = "World" $Greeting = "Hello, $Name!" $Greeting Which successfully prints Hello, World! . However, when I try to pass these strings as parameters to a function like so, function HelloWorld { Param ($Greeting, $Name) $Greeting } HelloWorld("Hello, $Name!", "World") I get the output Hello, ! World Upon investigation,