How do I create an array of integers from a variable containing the string "3 4 5 4 3"?
2 Answers
I prefer:
[int[]] -split "3 4 5 4 3"
-split handles whitespace space better than String.Split(). With String.Split(), if there is more than one space between numbers you wind up with empty strings in the generated array. The empty strings are coerced to 0 by PowerShell e.g.:
C:\PS> [int[]]"3 4 5 4 3".Split()
3
4
5
0
4
0
0
3