在PowerShell的空测试:为什么测试一个空数组从一个空字符串不同的表现? [英] Testing for null in Powershell: Why does testing an empty array behave differently from an empty string?

查看:240
本文介绍了在PowerShell的空测试:为什么测试一个空数组从一个空字符串不同的表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个code

 类型:array
$ V = @()
-eq`$空:$($ V $ -eq空)
-ne`$空:$($ V $ -ne空)类型:字符串
$ V =''
-eq`$空:$($ V $ -eq空)
-ne`$空:$($ V $ -ne空)

产生以下结果:

 类型:Array
  -eq $空:
  -ne $空:
类型:字符串
  -eq $空:假
  -ne $零:真


  1. 为什么 -eq -ne 像预期的那样一个空字符串,但要进行评估null或为空空数组?


  2. 更​​重要的是,你怎么区分和 $空之间的空数组?



解决方案

在比较运营商正在对一个集合使用,他们不返回一个布尔值($ true或$ false)。他们返回满足条件的集合中的所有成员。

请参阅:

获取帮助参阅about_Comparison_Operators

Consider this code

"Type: array"
$v = @()
"  -eq `$null: $($v -eq $null)"
"  -ne `$null: $($v -ne $null)"

"Type: string"
$v = ''
"  -eq `$null: $($v -eq $null)"
"  -ne `$null: $($v -ne $null)"

which produces the following result:

Type: array
  -eq $null: 
  -ne $null: 
Type: string
  -eq $null: False
  -ne $null: True

  1. Why does -eq and -ne behave as expected for an empty string, but evaluate to null or empty for an empty array?

  2. More importantly, how do you distinguish between $null and an empty array?

解决方案

When comparison operators are used against a collection, they do not return a boolean value ($true or $false). They return all the members of the collection that meet the condition.

See:

Get-help About_Comparison_Operators

这篇关于在PowerShell的空测试:为什么测试一个空数组从一个空字符串不同的表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆