string-interpolation

Python string interpolation using dictionary and strings

自古美人都是妖i 提交于 2019-12-03 19:03:35
问题 Given: dict = {"path": "/var/blah"} curr = "1.1" prev = "1.0" What's the best/shortest way to interpolate the string to generate the following: path: /var/blah curr: 1.1 prev: 1.0 I know this works: str = "path: %(path)s curr: %(curr)s prev: %(prev)s" % {"path": dict["path"],"curr": curr, "prev": prev} But I was hoping there is a shorter way, such as: str = "path: %(path)s curr: %s prev: %s" % (dict, curr, prev) My apologies if this seems like an overly pedantic question. 回答1: Why not: mystr

ruby 1.9 how to convert array to string without brackets

蹲街弑〆低调 提交于 2019-12-03 10:48:25
My question is about how to convert array elements to string in ruby 1.9 without getting the brackets and quotation marks. I've got an array (DB extract), from which I want to use to create a periodic report. myArray = ["Apple", "Pear", "Banana", "2", "15", "12"] In ruby 1.8 I had the following line reportStr = "In the first quarter we sold " + myArray[3].to_s + " " + myArray[0].to_s + "(s)." puts reportStr Which produced the (wanted) output In the first quarter we sold 2 Apple(s). The same two lines in ruby 1.9 produce (not wanted) In the first quarter we sold ["2"] ["Apple"] (s). After

Ember Interpolation in a href tag in handlebars template

倖福魔咒の 提交于 2019-12-03 08:04:34
I am trying to make a simple link to google maps with a dynamic address inserted into the href field. I have tried the code below plus tons of other messing around with no luck. How do you interpolate a dynamic ember string in a handlebars href field? I am using ember,rails, and handlebars. I know how to use bindAttr if I had the entire url stored with my model but I only have the address. Putting the google url with every model seemed unnecessary if i could just call it once in the view. <h1>{{name}}</h1> <div> <p><a {{bindAttr href='http://maps.com/?1=address'}}>{{address}}</a></p> </div>

Why does string interpolation is not working with const strings

自闭症网瘾萝莉.ら 提交于 2019-12-03 05:29:28
Why does string interpolation in c# does not work with const strings? For example: private const string WEB_API_ROOT = "/private/WebApi/"; private const string WEB_API_PROJECT = $"{WEB_API_ROOT}project.json"; From my point of view, everything is known at compile time. Or is that a feature that will be added later? Compiler message: The expression being assigned to 'DynamicWebApiBuilder.WEB_API_PROJECT' must be constant. Thanks a lot! Interpolated strings are simply converted to calls to string.Format . So your above line actually reads private const string WEB_API_PROJECT = string.Format("{0

How does string interpolation work in Kotlin?

微笑、不失礼 提交于 2019-12-03 01:12:19
Does the Kotlin compiler translate "Hello, $name!" using something like java.lang.String.format("Hello, %s!", name) or is there some other mechanism? And if I have a class like this for example: class Client { val firstName: String val lastName: String val fullName: String get() = "$firstName $lastName" } Will this getter return a cached string or will it try to build a new string? Should I use lazyOf delegate instead? I know that there will be no performance issue unless there will be millions of calls to fullName , but I haven't found documentation about this feature except for how to use it

What's the difference between raw string interpolation and triple quotes in scala

六月ゝ 毕业季﹏ 提交于 2019-12-02 16:33:10
Scala has triple quoted strings """String\nString""" to use special characters in the string without escaping. Scala 2.10 also added raw"String\nString" for the same purpose. Is there any difference in how raw"" and """""" work? Can they produce different output for the same string? Looking at the source for the default interpolators (found here: https://github.com/scala/scala/blob/2.11.x/src/library/scala/StringContext.scala ) it looks like the "raw" interpolator calls the identity function on each letter, so what you put in is what you get out. The biggest difference that you will find is

Interpolating regexes into another regex

北战南征 提交于 2019-12-02 03:19:47
问题 In the following code, k2 is minimally different from k1 . That is, k2 is exactly the same except that it's defined using an interpolation. (That is, I expected it to be exactly the same; Obviously from the result of p k2 it is not.) v = /[aeiouAEIOUäöüÄÖÜ]/ # vowels k1 = /[[ßb-zB-Z]&&[^[aeiouAEIOUäöüÄÖÜ]]]/ # consonants defined without interpolation k2 = /[[ßb-zB-Z]&&[^#{v}]]/ # consonants defined same way, but with interpolation But as below, using gsub with k1 works, whereas using it with

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

喜你入骨 提交于 2019-12-01 22:52:51
This question already has an answer here: How to use the ternary operator inside an interpolated string? 1 answer 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? From MSDN (emphasis mine): $"{person.Name, 20} is {person.Age:D3} year {(p.Age == 1 ? "" : "s")} old." You do not need to quote the quotation characters within the contained interpolation expressions because

How can I do string interpolation in JavaScript?

送分小仙女□ 提交于 2019-12-01 18:08:47
Consider this code: var age = 3; console.log("I'm " + age + " years old!"); Are there any other ways to insert the value of a variable in to a string, apart from string concatenation? Since ES6, you can use template literals : let age = 3 console.log(`I'm ${age} years old!`) P.S. Note the use of backticks: `` . tl;dr Use ECMAScript 2015's Template String Literals, if applicable. Explanation There is no direct way to do it, as per ECMAScript 5 specifications, but ECMAScript 6 has template strings , which were also known as quasi-literals during the drafting of the spec. Use them like this: >

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

懵懂的女人 提交于 2019-12-01 15:19:43
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? If you are experiencing this error in a .NET Framework 4.5.1 project, upgrading to 4.5.2 solves the problem. You have to encapsulate it with braces like this: EDIT: I updated because there was a missing curly. var extPropLookupNameCompania = $("{extension_{SettingsHelper