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

前端 未结 7 647
栀梦
栀梦 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:05

    $source = "number35"
    
    $number=$null
    
    $result = foreach ($_ in $source.ToCharArray()){$digit="0123456789".IndexOf($\_,0);if($digit -ne -1){$number +=$\_}}[int32]$number
    

    Just feed it digits and it wil convert to an Int32

提交回复
热议问题