重复字符在PowerShell中不起作用

问题描述 投票:2回答:1

我想在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
1个回答
10
投票

更改:

Write-Host "-" * $test.length

至:

Write-Host $("-" * $test.length)

在参数模式中,解析器将"-" * $test.length解释为三个单独的可扩展字符串 - 将它们包含在子表达式($())中,以便在将其绑定为参数参数之前对整个表达式进行求值。

你可能想要Get-Help about_Parsing

© www.soinside.com 2019 - 2024. All rights reserved.