PoshCode Archive  Artifact [74ce46eff1]

Artifact 74ce46eff1516f7f3a26476cbfa6eb2e529b305532eb7bd285355d66c9a92696:

  • File Join-String.ps1 — part of check-in [bd2e83a607] at 2018-06-10 13:11:46 on branch trunk — Joins array elements together using a specific string separator (user: Joel Bennett size: 1427)

# 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"
   }
}