views:

459

answers:

2
+5  Q: 

Powershell Joins

Hi Guys,

Im trying to join a number of elements of an array into a string using this;

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

But I get a 'Missing ')' in method call' error. The join documentation only mentions joining all the elements of an array, not elements at specific indexes. Can this be done?

Cheers

Andy

+9  A: 

Wrap the content of "$a[0,1,2,3]" with "$()" or "()"

PS> [string]::join("", $($a[0,1,2,3]))
hell
PS> [string]::join("", ($a[0,1,2,3]))
hell

-- Or --

you can use range operator ".."

PS> [string]::join("", $a[0..3])
hell
Sung Meister
Cheers Sung, thats awesome!
You're welcome. No. it's Powershell that's awesome! ;)
Sung Meister
+6  A: 

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

Shay Levy
+1; $OFS: I can't belive that I haven't thought of it ;)
Sung Meister