能否在 PowerShell 扩展字符串中直接使用格式说明符?
Can you use format specifiers directly in PowerShell expanding string?
在 PowerShell 中,我可以使用扩展字符串将变量值插入字符串 (Answer with more details about that):
PS C:\> $UtcNow = [DateTime]::UtcNow
PS C:\> "UtcNow = $UtcNow"
UtcNow = 08/10/2020 09:31:51
我可以使用 String.Format
或 -f
运算符来做,但我可以提供格式说明符:
PS C:\> "UtcNow = {0:u}" -f $UtcNow
UtcNow = 2020-08-10 09:31:51Z
PS C:\> [string]::Format("UtcNow = {0:u}", $UtcNow)
UtcNow = 2020-08-10 09:31:51Z
问题:有没有办法在和扩展字符串中使用格式说明符?
我试过的
PS C:\> "UtcNow = $UtcNow:u" # Doesn't work
UtcNow =
PS C:\> "UtcNow = ${UtcNow:u}" # Doesn't work
UtcNow =
PS C:\> "UtcNow = $($UtcNow.ToString("u"))" # Works, but is quite cumbersome
UtcNow = 2020-08-10 09:31:51Z
不,PowerShell 中的可扩展字符串不计算格式说明符
-f
是要走的路:)
在 PowerShell 中,我可以使用扩展字符串将变量值插入字符串 (Answer with more details about that):
PS C:\> $UtcNow = [DateTime]::UtcNow
PS C:\> "UtcNow = $UtcNow"
UtcNow = 08/10/2020 09:31:51
我可以使用 String.Format
或 -f
运算符来做,但我可以提供格式说明符:
PS C:\> "UtcNow = {0:u}" -f $UtcNow
UtcNow = 2020-08-10 09:31:51Z
PS C:\> [string]::Format("UtcNow = {0:u}", $UtcNow)
UtcNow = 2020-08-10 09:31:51Z
问题:有没有办法在和扩展字符串中使用格式说明符?
我试过的
PS C:\> "UtcNow = $UtcNow:u" # Doesn't work
UtcNow =
PS C:\> "UtcNow = ${UtcNow:u}" # Doesn't work
UtcNow =
PS C:\> "UtcNow = $($UtcNow.ToString("u"))" # Works, but is quite cumbersome
UtcNow = 2020-08-10 09:31:51Z
不,PowerShell 中的可扩展字符串不计算格式说明符
-f
是要走的路:)