Split & Trim in a single step
TheMadTechnician has provided the crucial pointer in a comment on the question:
Use the -split
operator, which works the same in PSv2: It expects a regular expression (regex) as the separator, allowing for more sophisticated tokenizing than the [string]
type's .Split()
method, which operates on literals:
PS> 'One, Two, Three' -split ',\s*' | ForEach-Object { "[$_]" }
[One]
[Two]
[Three]
Regex ,\s*
splits the input string by a comma followed by zero or more (*
) whitespace characters (\s
).
In fact, choosing -split
over .Split()
is advisable in general, even in later PowerShell versions.
However, to be fully equivalent to the .Trim()
-based solution in the question, trimming of leading and trailing whitespace is needed too:
PS> ' One, Two,Three ' -split ',' -replace '^\s+|\s+$' | ForEach-Object { "[$_]" }
[One]
[Two]
[Three]
-replace '^\s+|\s+$'
removes the leading and trailing whitespace from each token resulting from the split: |
specifies an alternation so that the subexpressions on either side of it are considered a match; ^\s+,
matches leading whitespace, \s+$
matches trailing whitespace; \s+
represents a non-empty (one or more, +
) run of whitespace characters; for more information about the -replace
operator, see this answer.
(In PSv3+, you could simplify to (' One, Two,Three ' -split ',').Trim()
or use the solution from the question.)
As for why ('One, Two, Three'.Split(',')).Trim()
doesn't work in PSv2: The .Split()
method returns an array of tokens, and invoking the .Trim()
method on that array - as opposed to its elements - isn't supported in PSv2.
In PSv3+, the .Trim()
method call is implicitly "forwarded" to the elements of the resulting array, resulting in the desired trimming of the individual tokens - this feature is called member enumeration.
I don't have PS 2.0 but you might try something like
$string = 'One, Two, Three'
$array = ($string.Split(',') | % { $_.Trim() })
and see if that suits. This is probably less help for you but for future readers who have moved to future versions you can use the #Requires
statement. See help about_Requires
to determine if your platforms supports this feature.