Reputation:
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
Upvotes: 10
Views: 2647
Reputation: 165
More idiomatic: use PowerShell's built-in join operator like this:
PS> $a[0,1,2,3] -join ""
hell
Upvotes: 1
Reputation: 36955
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
Upvotes: 13