How do I create an array of integers from a variable containing the string "3 4 5 4 3"?
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
Splitting the string creates an array of string, add a cast to an array of integers:
[int[]]"3 4 5 4 3".Split()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With