schittli
12/14/2013 - 8:04 AM

three different ways of splitting a comma separated string and trimming the items using PowerShell

three different ways of splitting a comma separated string and trimming the items using PowerShell

$CSVData = @("One       ,   Two ,   Three    ")

#just split
$Data = $CSVData -split (",")

#first way of split and trim
$Data = $CSVData -split ',' | foreach {$_.Trim()}

#second way of split and trim
$CSVData.Split(",").Trim()

#third way of split and trim using regular expression
$Data = $CSVData -split '\s*,\s*'