简体   繁体   中英

Powershell: conditional join delimiter for empty array (ignore delimiter if empty)?

I'm new to PowerShell and could not find a proper answer to this one:

I'm building a script that may require some manual modification at times. There is an array that is giving me grief when it is empty due to a join. I am not sure how to evaluate the array for when it is empty while preserving my lazy delimiter.

$array = @() -join '|'

In this case: '$array.count' returns 1, whereas I'd prefer to skip the -join if empty and return '0'. Is there a way to make the join conditional based on a populated array?

Maybe you could add a conditional somewhere in your code to make it work. If the array count is equals to one, in your case, it means it is empty. For example:

if($array.count -le 1) { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM