gpt4 book ai didi

variables - 变量扩展和转义字符

转载 作者:行者123 更新时间:2023-12-04 09:04:30 24 4
gpt4 key购买 nike

在PowerShell中,您可以在字符串中扩展变量,如下所示:

$myvar = "hello"
$myvar1 = "$myvar`world" #without the `, powershell would look for a variable called $myvarworld
Write-Host $myvar1 #prints helloworld

我遇到的问题是转义字符,例如 n r等,如下所示:
$myvar3 = "$myvar`albert"
Write-Host $myvar3 #prints hellolbert as `a is an alert

还以下不起作用:
$myvar2 = "$myvar`frank" #doesnt work
Write-Host $myvar2 #prints hellorank.

问题:
使用自动变量扩展功能时,如何组合字符串而不必担心转义字符?
还是只需要这样:
$myvar = "hello"
$myvar1 = "$myvar"+"world" #using +
Write-Host $myvar1
$myvar2 = "$myvar"+"frank" #using +

最佳答案

这种方式尚未提及:

"$($myvar)frank"

和这个:
"${myvar}frank"

关于variables - 变量扩展和转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6586364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com