In Powershell what is the idiomatic way of converting a string to an int?

前端 未结 7 649
栀梦
栀梦 2020-12-05 12:32

The only method I have found is a direct cast:

> $numberAsString = \"10\"
> [int]$numberAsString
10

Is this the standard approach in

7条回答
  •  死守一世寂寞
    2020-12-05 13:21

    I'd probably do something like that :

    [int]::Parse("35")
    

    But I'm not really a Powershell guy. It uses the static Parse method from System.Int32. It should throw an exception if the string can't be parsed.

提交回复
热议问题