Powershell加入

10 string powershell join

我试图使用这个将数组的多个元素连接成一个字符串;

$a = "h","e","l","l","o"
$b = [string]::join("", $a[0,1,2,3])
Run Code Online (Sandbox Code Playgroud)

但是我在"方法调用"错误中得到了"丢失".连接文档仅提及连接数组的所有元素,而不是特定索引处的元素.可以这样做吗?

干杯

安迪

dan*_*die 13

用"$()"或"()"换行"$ a [0,1,2,3]"的内容

PS> [string]::join("", $($a[0,1,2,3]))
hell
PS> [string]::join("", ($a[0,1,2,3]))
hell
Run Code Online (Sandbox Code Playgroud)

- 要么 -

你可以使用范围运算符".."

PS> [string]::join("", $a[0..3])
hell
Run Code Online (Sandbox Code Playgroud)


Sha*_*evy 8

PS > & {$ofs=""; "$($a[0,1,2,3])"}  
hell
Run Code Online (Sandbox Code Playgroud)