February 2019
Intermediate to advanced
626 pages
15h 51m
English
The Split method has a relative in PowerShell: the -split operator. The -split operator expects a regular expression, whereas the split method for a string expects an array of characters by default:
$myString = 'Surname,GivenName'
$myString.Split(',')
When splitting the following string based on a comma, the resulting array will have three elements. The first element is Surname, the last is GivenName. The second element in the array (index 1) is blank:
$string = 'Surname,,GivenName'
$array = $string.Split(',')
$array.Count # This is 3
$array[1] # This is empty
This blank value may be discarded by setting the StringSplitOptions argument of the Split method:
$string = 'Surname,,GivenName' $array = $string.Split(',', [StringSplitOptions]::RemoveEmptyEntries) ...Read now
Unlock full access