题
我尝试使用此函数将数组的多个元素连接成一个字符串;
$a = "h","e","l","l","o"
$b = [string]::join("", $a[0,1,2,3])
但是我在方法调用'错误中得到'Missing')'连接文档仅提及连接数组的所有元素,而不是特定索引处的元素。可以这样做吗?
干杯
安迪
解决方案
包含“$ a [0,1,2,3]”的内容用“$()”或“()”
PS> [string]::join("", $($a[0,1,2,3]))
hell
PS> [string]::join("", ($a[0,1,2,3]))
hell
- 或 -
您可以使用范围运算符“..”"
PS> [string]::join("", $a[0..3])
hell
其他提示
PS > & {$ofs=""; "$($a[0,1,2,3])"}
hell
更多惯用法:使用PowerShell的内置连接运算符,如下所示:
PS> $a[0,1,2,3] -join ""
hell
不隶属于 StackOverflow