在 Python 中,我想创建一个带有嵌入式表达式的字符串块。
在 Ruby 中,代码如下所示:
def get_val
100
end
def testcode
s=<<EOS
This is a sample string that references a variable whose value is: #{get_val}
Incrementing the value: #{get_val + 1}
EOS
puts s
end
testcode
Run Code Online (Sandbox Code Playgroud) 鉴于以下测试代码,为什么字符串插值\(t1)和\(t2)不起作用?重命名description会导致编译器失败,"类型'测试'不符合协议'可打印'",并且description显然可以在给定每个测试的第二个println时使用.
enum Test: Printable {
case A
case B(Int)
var description: String {
switch self {
case .A:
return ".A"
case let .B(value):
return ".B: value = \(value)"
}
}
}
let t1 = Test.A
let t2 = Test.B(-100)
println("t1 = \(t1)") // prints "t1 = (Enum Value)"
println(t1.description) // prints ".A"
println("t2 = \(t2)") // prints "t2 = (Enum Value)"
println(t2.description) // prints ".B: value = -100"
Run Code Online (Sandbox Code Playgroud) 在python我能做到
_str = "My name is {}"
...
_str = _str.format("Name")
Run Code Online (Sandbox Code Playgroud)
在红宝石中,当我尝试
_str = "My name is #{name}"
Run Code Online (Sandbox Code Playgroud)
解释器抱怨变量名是未定义的,所以它是期望的
_str = "My name is #{name}" => {name =: "Name"}
Run Code Online (Sandbox Code Playgroud)
如何在ruby中使用字符串占位符供以后使用?
特定
IDictionary<string, string> x;
Run Code Online (Sandbox Code Playgroud)
以前你可以做(作为带引号的参数代码示例):
string.Format("{0}", x["y"]);
Run Code Online (Sandbox Code Playgroud)
格式化C#6.0字符串插值的正确方法是什么?
$"{x["y"]}" // compiler error due to the quotes on the indexer value
// UPDATE: actually does work, must have had another typo I missed
Run Code Online (Sandbox Code Playgroud)
将引号转义为
\"
Run Code Online (Sandbox Code Playgroud)
不行,做
var b = "y";
...
$"{x[b]}"
Run Code Online (Sandbox Code Playgroud)
看起来很尴尬.
我有一个包含一些插值语句的字符串,比如description='Hello! I am #{self.name}'.该字符串按原样存储在数据库中; 所以没有应用自动字符串插值.
我想在以后应用这种插值.我可以做一些疯狂的事情eval("\"+description+\"")但是必须有一个更好,更红宝石般的方式来做到这一点,对吧?
我希望在字符串插值语句中使用"{",但是我很难找到转义符来执行它.
var val = "ERROR_STATE";
var str = $"if(inErrorState){ send 1,\"{val}\" }"
Run Code Online (Sandbox Code Playgroud)
期望的输出:
if(inErrorState){send 1,"ERROR_STATE"}
Run Code Online (Sandbox Code Playgroud)
简单的解决方案是不使用字符串插值,但我认为这样做更容易阅读.
我喜欢C#的新插值语法.
我想在我的.config文件中存储一个动态字符串,appSettings并在其上应用插值.期望值interpolatedMessage是"Your User Name is SnowWhite"
如果可能的话会很棒.这将帮助我保持密钥可配置为任何组合.(例如"SnowWhite是您的用户名","用户名 - SnowWhite")
<appSettings>
<add key="userNameKey" value="Your User Name is {userName}" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
test.sh不会替换test.json参数值($ input1和$ input2)。result.json具有相同的ParameterValue“ $ input1 / solution / $ input2.result”
[
{
"ParameterKey": "Project",
"ParameterValue": [ "$input1/solution/$input2.result" ]
}
]
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
input1="test1"
input2="test2"
echo $input1
echo $input2
cat test.json | jq 'map(if .ParameterKey == "Project" then . + {"ParameterValue" : "$input1/solution/$input2.result" } else . end )' > result.json
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个函数,该函数将打印用户提供的问候语,该问候语发送给用户提供的名称.我想在这个代码块中使用扩展字符串:
$Name = "World"
$Greeting = "Hello, $Name!"
$Greeting
Run Code Online (Sandbox Code Playgroud)
哪个成功打印Hello, World!.但是,当我尝试将这些字符串作为参数传递给类似的函数时,
function HelloWorld
{
Param ($Greeting, $Name)
$Greeting
}
HelloWorld("Hello, $Name!", "World")
Run Code Online (Sandbox Code Playgroud)
我得到了输出
Hello, !
World
Run Code Online (Sandbox Code Playgroud)
经调查,PowerShell的似乎忽略了$Name在"Hello, $Name!"完全与运行
HelloWorld("Hello, !", "World")
Run Code Online (Sandbox Code Playgroud)
产生与上述相同的输出.此外,它似乎并不认为"World"是$Name运行以来 的价值
function HelloWorld
{
Param ($Greeting, $Name)
$Name
}
HelloWorld("Hello, $Name!", "World")
Run Code Online (Sandbox Code Playgroud)
不产生任何产出.
当作为函数参数传入时,有没有办法让扩展字符串工作?
虽然我没有找到它的文档,显然你可以通过{}插入它们周围的无形变量和常量:
constant ? = " " xx 4;
say "{?}Tabbed"; # OUTPUT: « Tabbed?»
Run Code Online (Sandbox Code Playgroud)
显然,您需要使用:c副词来执行此操作.
say q:c"? is {?}"; # OUTPUT: «? is 3.141592653589793?»
Run Code Online (Sandbox Code Playgroud)
但这只是在使用if q(和派生)引号的情况下.这只是一个没有文档记录的功能,还是我在这里缺少的东西?