string-interpolation

String concatenation vs. interpolation in Ruby

我怕爱的太早我们不能终老 提交于 2019-11-26 08:09:59
问题 I am just starting to learn Ruby (first time programming), and have a basic syntactical question with regards to variables, and various ways of writing code. Chris Pine\'s \"Learn to Program\" taught me to write a basic program like this... num_cars_again= 2 puts \'I own \' + num_cars_again.to_s + \' cars.\' This is fine, but then I stumbled across the tutorial on ruby.learncodethehardway.com, and was taught to write the same exact program like this... num_cars= 2 puts \"I own #{num_cars}

How do I interpolate strings?

空扰寡人 提交于 2019-11-26 06:38:30
问题 I want to do the following in C# (coming from a Python background): strVar = \"stack\" mystr = \"This is %soverflow\" % (strVar) How do I replace the token inside the string with the value outside of it? 回答1: string mystr = string.Format("This is {0}overflow", strVar); And you could also use named parameters instead of indexes. 回答2: This has been added as of C# 6.0 (Visual Studio 2015+). Example: var planetName = "Bob"; var myName = "Ford"; var formattedStr = $"Hello planet {planetName}, my

What does $($variableName) mean in expandable strings in PowerShell?

我是研究僧i 提交于 2019-11-26 06:38:03
问题 I\'ve seen a number of example scripts online that use this. Most recently, I saw it in a script on automating TFS: [string] $fields = \"Title=$($taskTitle);Description=$($taskTitle);Assigned To=$($assignee);\" $fields += \"Area Path=$($areaPath);Iteration Path=$($iterationPath);Discipline=$($taskDisciplineArray[$i]);Priority=$($i+1);\" $fields += \"Estimate=$($taskEstimateArray[$i]);Remaining Work=$($taskRemainingArray[$i]);Completed Work=$($tasktaskCompletedArray[$i])\" From what I can tell

Python 3 returns “invalid syntax” when trying to perform string interpolation

試著忘記壹切 提交于 2019-11-26 05:58:49
问题 I have recently been learning python 3 and I cannot get any examples involving string interpolation (formatting) to work. In [1]: state = \"Washington\" In [2]: state Out[2]: \'Washington\' In [3]: my_message = f\"I live in {state}\" File \"<ipython-input-3-d004dd9e0255>\", line 1 my_message = f\"I live in {state}\" ^ SyntaxError: invalid syntax I figured my machine was defaulting to python 2, but a quick check reveals: Python 3.5.2 (default, Nov 17 2016, 17:05:23) Type \"copyright\", \

Why does string interpolation work in Ruby when there are no curly braces?

十年热恋 提交于 2019-11-26 04:45:30
问题 The proper way to use string interpolation in Ruby is as follows: name = \"Ned Stark\" puts \"Hello there, #{name}\" #=> \"Hello there, Ned Stark\" That is the way I intend to always use it. However, I\'ve noticed something odd in Ruby\'s string interpolation. I\'ve noticed that string interpolation works in Ruby without the curly braces in regards to instance variables. For example: @name = \"Ned Stark\" puts \"Hello there, #@name\" #=> \"Hello there, Ned Stark\" And that trying the same

How to use the ternary operator inside an interpolated string?

半腔热情 提交于 2019-11-26 04:38:45
问题 I\'m confused as to why this code won\'t compile: var result = $\"{fieldName}{isDescending ? \" desc\" : string.Empty}\"; If I split it up, it works fine: var desc = isDescending ? \" desc\" : string.Empty; var result = $\"{fieldName}{desc}\"; 回答1: According to the documentation: The structure of an interpolated string is as follows: { <interpolationExpression>[,<alignment>][:<formatString>] } The problem is that the colon is used to denote formatting, like: Console.WriteLine($"The current

Is there a Python equivalent to Ruby&#39;s string interpolation?

。_饼干妹妹 提交于 2019-11-26 03:19:09
问题 Ruby example: name = \"Spongebob Squarepants\" puts \"Who lives in a Pineapple under the sea? \\n#{name}.\" The successful Python string concatenation is seemingly verbose to me. 回答1: Python 3.6 will add literal string interpolation similar to Ruby's string interpolation. Starting with that version of Python (which is scheduled to be released by the end of 2016), you will be able to include expressions in "f-strings", e.g. name = "Spongebob Squarepants" print(f"Who lives in a Pineapple under

PowerShell outputting array items when interpolating within double quotes

六眼飞鱼酱① 提交于 2019-11-26 01:54:54
问题 I found some strange behavior in PowerShell surrounding arrays and double quotes. If I create and print the first element in an array, such as: $test = @(\'testing\') echo $test[0] Output: testing Everything works fine. But if I put double quotes around it: echo \"$test[0]\" Output: testing[0] Only the $test variable was evaluated and the array marker [0] was treated literally as a string. The easy fix is to just avoid interpolating array variables in double quotes, or assign them to another

PHP - how to create a newline character?

白昼怎懂夜的黑 提交于 2019-11-26 01:31:25
问题 In PHP I am trying to create a newline character: echo $clientid; echo \' \'; echo $lastname; echo \' \'; echo \'\\r\\n\'; Afterwards I open the created file in Notepad and it writes the newline literally: 1 John Doe\\r\\n 1 John Doe\\r\\n 1 John Doe\\r\\n I have tried many variations of the \\r\\n , but none work. Why isn\'t the newline turning into a newline? 回答1: Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want: "\r\n"

How to interpolate variables in strings in JavaScript, without concatenation?

穿精又带淫゛_ 提交于 2019-11-25 23:06:14
问题 I know in PHP we can do something like this: $hello = \"foo\"; $my_string = \"I pity the $hello\"; Output: \"I pity the foo\" I was wondering if this same thing is possible in JavaScript as well. Using variables inside strings without using concatenation — it looks more concise and elegant to write. 回答1: Starting from Firefox 34 / Chrome 41 / Safari 9 / Microsoft Edge you can use an ES2015 / ES6 feature called Template Literals and use this syntax: `String text ${expression}` Template