Count length of array and return 1 if it only contains one element

前端 未结 4 1957
轻奢々
轻奢々 2020-12-09 15:33
$cars = \"bmw\",\"audi\",\"volvo\",\"vw\"
echo $cars.length

returns 4, but

$cars = \"bmw\"

returns 3 because it

4条回答
  •  借酒劲吻你
    2020-12-09 16:14

    A couple other options:

    1. Use the comma operator to create an array:

      $cars = ,"bmw"
      $cars.GetType().FullName
      # Outputs: System.Object[]
      
    2. Use array subexpression syntax:

      $cars = @("bmw")
      $cars.GetType().FullName
      # Outputs: System.Object[]
      

    If you don't want an object array you can downcast to the type you want e.g. a string array.

     [string[]] $cars = ,"bmw"
     [string[]] $cars = @("bmw")
    

提交回复
热议问题