如何在 PowerShell 中将数组对象转换为字符串?
问题描述:
如何将数组对象转换为字符串?
How can I convert an array object to string?
我试过了:
$a = "This", "Is", "a", "cat"
[system.String]::Join(" ", $a)
没有运气.PowerShell 中有哪些不同的可能性?
with no luck. What are different possibilities in PowerShell?
答
$a = 'This', 'Is', 'a', 'cat'
使用双引号(并可选择使用分隔符 $ofs
)
Using double quotes (and optionally use the separator $ofs
)
# This Is a cat
"$a"
# This-Is-a-cat
$ofs = '-' # after this all casts work this way until $ofs changes!
"$a"
使用运算符加入
# This-Is-a-cat
$a -join '-'
# ThisIsacat
-join $a
使用转换为[string]
# This Is a cat
[string]$a
# This-Is-a-cat
$ofs = '-'
[string]$a