forked from RamblingCookieMonster/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Join-Parts.ps1
65 lines (45 loc) · 1.46 KB
/
Join-Parts.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function Join-Parts
{
<#
.SYNOPSIS
Join strings with a specified separator.
.DESCRIPTION
Join strings with a specified separator.
This strips out null values and any duplicate separator characters.
See examples for clarification.
.PARAMETER Separator
Separator to join with
.PARAMETER Parts
Strings to join
.EXAMPLE
Join-Parts -Separator "/" this //should $Null /work/ /well
# Output: this/should/work/well
.EXAMPLE
Join-Parts -Parts http://this.com, should, /work/, /wel
# Output: http://this.com/should/work/wel
.EXAMPLE
Join-Parts -Separator "?" this ?should work ???well
# Output: this?should?work?well
.EXAMPLE
$CouldBeOneOrMore = @( "JustOne" )
Join-Parts -Separator ? -Parts CouldBeOneOrMore
# Output JustOne
# If you have an arbitrary count of parts coming in,
# Unnecessary separators will not be added
.NOTES
Credit to Rob C. and Michael S. from this post:
http://stackoverflow.com/questions/9593535/best-way-to-join-parts-with-a-separator-in-powershell
#>
[cmdletbinding()]
param
(
[string]$Separator = "/",
[parameter(ValueFromRemainingArguments=$true)]
[string[]]$Parts = $null
)
( $Parts |
Where { $_ } |
Foreach { ( [string]$_ ).trim($Separator) } |
Where { $_ }
) -join $Separator
}