# encoding: ascii
# api: powershell
# title: Join-String
# description: Joins array elements together using a specific string separator
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Join-String
# x-poshcode-id: 2516
# x-archived: 2015-04-20T01:13:57
# x-published: 2011-02-23T07:41:00
#
# This function ‘join’ works in the pipeline, roughly the same way as the -join operator (from v2) doesn’t. Additionally it has -prepend and -append parameters which lets you use it to add multiple items to an existing list, and a -Unique switch to remove duplicates.
# Important Note: It does not preserve empty items, nor respect quote-escaped separators.
#
#.Synopsis
# Joins array elements together using a specific string separator
#.Example
# $Env:Path = ls | ? {$_.PSIsContainer} | Select -expand FullName | Join ";" -Append $Env:Path
#.Example
# get-process | select -expand name | join ","
#
function Join-String {
param ( [string]$separator, [string]$append, [string]$prepend, [string]$prefix, [string]$postfix, [switch]$unique )
begin { [string[]]$items = @($prepend.split($separator)) }
process { $items += $_ }
end {
$ofs = $separator;
$items += @($append.split($separator));
if($unique) {
$items = $items | Select -Unique
}
return "$prefix$($items -ne '')$postfix"
}
}