Powershell split string on multiple conditions -
i have csv file extracting first , last name column, seperate them in own columns.
so given string:
'john doe - generic- random'
i use split(" ")
extract first , last name
$string = 'john doe - generic- random' $firstname = $string.split(" ")[0] $lastname = $string.split(" ")[1]
first issue
i found issue after last name, string not have space. example
$string = 'john doe-generic-random'
how last name doe
out rest. how can apply split 2 conditions of " "
, "-"
2nd issue
some strings have first name. example...
$string = 'john - generic - random
how can assign last name $null if case?
looks working...
$string = 'john doe - generic- random' $firstname = $string -split {$_ -eq " " -or $_ -eq "-"} $firstname[0] $lastname = $string -split {$_ -eq " " -or $_ -eq "-"} $firstname[1]
if there no last name, blank in column last name.
if there last name no space between lastname-info, exclude -info , add lastname last name column.
Comments
Post a Comment