我想在PowerShell中重复一个字符。例如:
$test = "this is a test that I want to underline with --------"
Write-Host $test
Write-Host "-" * $test.length
然而,使用上述代码的输出为:
This is a test that I want to underline with --------
- * 53
我有没有漏掉什么非常明显的东西?
我想在PowerShell中重复一个字符。例如:
$test = "this is a test that I want to underline with --------"
Write-Host $test
Write-Host "-" * $test.length
然而,使用上述代码的输出为:
This is a test that I want to underline with --------
- * 53
我有没有漏掉什么非常明显的东西?
改变:
Write-Host "-" * $test.length
至:
Write-Host $("-" * $test.length)
在参数模式下,解析器将"-" * $test.length
解释为三个独立的可扩展字符串 - 将它们括在子表达式($()
)中,以便在将其绑定为参数参数之前对整个表达式进行求值。
您可能需要Get-Help about_Parsing
(获取关于解析的帮助)。
write-host ("-" * $test.length)
应该足够。 - JosefZabout_Parsing
也描述了这一点) - Mathias R. Jessen